1
我試圖改變由說出的單詞設置的文本字段中單詞的顏色(例如:開心,傷心,生氣等)。 如果該單詞被多次說出,則它不起作用。例如,如果我說:「我感到高興,因爲我的貓對我很好,我的兄弟讓我難過,我很高興。」它只會改變第一個'快樂'的顏色,我不完全確定爲什麼。Swift 3.0 Speech to Text:改變單詞的顏色
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
}
謝謝!
非常感謝你爲這個偉大的** **的答案! – Mariella