Swift 3归因于文本的解决方案

时间:2017-07-06 13:49:47

标签: swift nsattributedstring

我有很长的字符串: “问:这是什么日子?\ nA:bla bla \ nQ:现在几点了?\ n:bla bla \ nQ:这是什么日子?\ n:bla bla \ nQ:现在几点了?\ n: bla bla \ n“

我的问题是我需要在textView中显示此文本,但问题必须是橙色,答案必须是灰色。我从服务器获取字符串,不知道我会得到什么,但这样的格式。如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

如果你的字符串格式总是像"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"那么你可以尝试下面的代码:

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

结果将是:

enter image description here

希望这会有所帮助。

答案 1 :(得分:0)

问题是在第一个问题之后没有“\ n”。 我通过另一种方式解决了这个问题:

    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