NSString *message = @"This part: \"Is in quotations\"";
我想知道是否可以为引号内的NSString
部分涂上不同的颜色?我理解这个例子:Is it possible to change color of single word in UITextView and UITextField但不知道如何将它转换为我的例子,因为我有多个slach。
答案 0 :(得分:1)
我已经写了一个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),或者可以在自己的操场上玩它