简体   繁体   中英

Swift 3 Attributed text solution

I have long string something like this: "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"

My problem is I need to show this text in textView but the question has to be orange color and answer has to be grey color. I get string from server and don't know what will I get but the format like this. How resolve this problem?

If your string formate is like "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" always then you can try below 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

And result will be:

在此处输入图片说明

Hope this will help.

The problem is there is no "\\n" after first question. I've solved this by a little another way:

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM