【发布时间】:2017-03-25 18:57:46
【问题描述】:
我正在尝试更改由口语设置的文本字段中单词的颜色(例如:快乐、悲伤、愤怒等)。 如果这个词被多次说出来,它就不起作用了。例如,如果我说:“我很开心,因为我的猫对我很好。我的兄弟让我伤心。我又开心了。”它只会改变第一个“快乐”的颜色,我不确定为什么。
func setTextColor(text: String) -> NSMutableAttributedString {
let string:NSMutableAttributedString = NSMutableAttributedString(string: text)
let words:[String] = text.components(separatedBy:" ")
for word in words {
if emotionDictionary.keys.contains(word) {
let range:NSRange = (string.string as NSString).range(of: word)
string.addAttribute(NSForegroundColorAttributeName, value: emotionDictionary[word], range: range)
}
}
return string
}
谢谢!
【问题讨论】:
标签: ios swift speech-to-text