我正在尝试更改以 @
开头的特定单词的颜色,但是我在下面的代码中不断收到错误消息。
据我所知,它似乎与范围变量有关。
我收到以下错误:
cannot invoke 'addAttribute' with an argument list of type (String, value: UIColor, range Range?)
代码:
var messageMutableString = NSMutableAttributedString(string: message, attributes: [NSFontAttributeName:UIFont(name: "PT Sans", size: 13.0)!])
var words = message.componentsSeparatedByString(" ")
for word in words {
if word.hasPrefix("@") {
var range = message.rangeOfString(word)
messageMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor(rgba: "#B52519"), range: range)
}
}
最佳答案
addAttribute
采用 NSRange
。尝试使用以下内容:
let range = (message as NSString).rangeOfString(word)
附注如果可能,最好使用 let
而不是 var
。
关于ios - 更改字符串中特定单词的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30422076/