2017-07-06 1 views
1

J'ai une longue chaîne quelque chose comme ceci: "Q: quel jour est-il? \ NA: bla bla \ nQ: quelle heure est-il? \ NA: bla bla \ nQ: quel jour est-il? ? \ nA: bla bla \ nQ: quelle heure est-il? \ nA: bla bla \ n "Swift 3 Solution de texte attributaire

Mon problème est que je dois afficher ce texte dans textView mais la question doit être de couleur orange et la réponse doit être couleur grise. Je reçois une chaîne du serveur et je ne sais pas ce que je vais obtenir, mais le format comme ça. Comment résoudre ce problème?

+0

La partie la plus difficile est de savoir le délimiteur exacte des questions: un Answsers 'Q: ... A:' 'jusqu'à ce que \ n' et suivant ? Pour le reste, c'est assez simple une fois que vous connaissez les gammes. – Larme

+0

Pouvez-vous fournir le délimiteur s'il vous plaît? Et aussi, êtes-vous sûr que vous pouvez TOUJOURS obtenir ce format? –

Répondre

0

Si votre formate de chaîne est comme toujours "Q: what day is it?\nA: bla bla\nQ: what time is it?\nA: bla bla\nQ: what day is it?\nA: bla bla\nQ: what time is it?\nA: bla bla\n" alors vous pouvez essayer ci-dessous le code:

var str = "Q: what day is it?\nA: bla bla\nQ: what time is it?\nA: bla bla\nQ: what day is it?\nA: bla bla\nQ: what time is it?\nA: bla bla\n" 
let textView = UITextView(frame: CGRect(x: 20.0, y: 90.0, width: 250.0, height: 200.0)) 

    textView.backgroundColor = UIColor.white 

    //separate it with \n 
    let QuestionsAndAnswersArray = str.components(separatedBy: ["\n"]) 

    var finalStringArr = NSMutableAttributedString() 

    for item in QuestionsAndAnswersArray { 

     if item.characters.count > 0 { 

      let firstChar = item[item.startIndex] 

      //For Questions 
      if firstChar == "Q" { 

       //For new line after every character 
       let QuestionStr = item + "\n" 

       var myMutableString = NSMutableAttributedString() 
       myMutableString = NSMutableAttributedString(string: QuestionStr) 
       myMutableString.setAttributes([NSForegroundColorAttributeName : UIColor.red], range: NSRange(location:0,length:item.characters.count)) 
       finalStringArr.append(myMutableString) 

      } else if firstChar == "A" { //For Answers 

       let answerStr = item + "\n" 

       //For new line after every character 
       var myMutableString = NSMutableAttributedString() 
       myMutableString = NSMutableAttributedString(string: answerStr) 
       myMutableString.setAttributes([NSForegroundColorAttributeName : UIColor.gray], range: NSRange(location:0,length:item.characters.count)) 
       finalStringArr.append(myMutableString) 
      } 
     } 
    } 

    textView.attributedText = finalStringArr 

Et résultat sera:

enter image description here

Espérons que cela aidera.

0

Le problème est qu'il n'y a pas de "\ n" après la première question. J'ai résolu ce problème en un peu une autre façon:

let faqStr = "Q: what day is it?A: bla bla\nQ: what time is it?\nA: bla bla\nQ: what day is it?\nA: bla bla\nQ: what time is it?\nA: bla bla\n" 
    var faqStrArray = faqStr.components(separatedBy: "Q:") //separate by Q+A 

    faqStrArray.remove(at: 0) // [0]="" 

    let attrText = NSMutableAttributedString() 
    for i in 0..<faqStrArray.count { 
     faqStrArray[i] = "Q:\(faqStrArray[i])" // append "Q:" that was taken by separator 

     var array = faqStrArray[i].components(separatedBy: "A:") // separate to Question and Answer 
     array[1] = "A:\(array[1])\n" // append "A:" that was taken by separator 

     let attrStringOrange: NSMutableAttributedString = NSMutableAttributedString(string: array[0]) // orange string 
     attrStringOrange.addAttribute(NSForegroundColorAttributeName, value: UIColor.orange, range: NSMakeRange(0, attrStringOrange.length)) // adding attribute 
     let attrStringGrey: NSMutableAttributedString = NSMutableAttributedString(string: array[1]) // grey string 
     attrStringGrey.addAttribute(NSForegroundColorAttributeName, value: UIColor.gray, range: NSMakeRange(0, attrStringGrey.length)) // adding attribute 

     attrStringOrange.append(attrStringGrey) // appending answer to question 

     attrText.append(attrStringOrange) 
    } 

    txtView.attributedText = attrText