更改引号内文本的颜色

Change color of text inside quotations

NSString *message = @"This part: \"Is in quotations\"";

我想知道是否可以将引号内的 NSString 部分涂成不同的颜色?我理解这个示例:Is it possible to change color of single word in UITextView and UITextField 但不确定如何将其转换为我的示例,因为我有多个斜杠。

我写了一个 xcode 游乐场示例(在 swift 因为我很懒惰),它会为引号内的任何内容着色

var s = "this is a string with \"quotations\" that has multiple \"quotations\" blah blah"

var split:[String] = s.componentsSeparatedByString("\"")

var att:NSMutableAttributedString = NSMutableAttributedString()

for i in 0..<split.count {

    var temp:NSMutableAttributedString

    if((i+1)%2 == 0){ //every even index is something between quotations because of how we split
        temp = NSMutableAttributedString(string: "\"" + split[i] + "\"")
        temp.addAttribute(NSForegroundColorAttributeName, value: UIColor.greenColor(), range: NSRange(location: 0, length: temp.string.characters.count))
    }
    else {
        temp = NSMutableAttributedString(string: split[i])
        temp.addAttribute(NSForegroundColorAttributeName, value: UIColor.whiteColor(), range: NSRange(location: 0, length: temp.string.characters.count))
    }

    att.appendAttributedString(temp)

}

结果:

我相信将它重新用于您的需要(和 Obj-c)会很容易,或者可以在您自己的游乐场中使用它