私は話し言葉(例:幸せ、悲しみ、怒りなど)によって設定されたテキストフィールドの単語の色を変更しようとしています。 単語が2回以上話されている場合は機能しません。たとえば、私の弟が私を悲しませている、私は再び幸せです。それは最初の「幸せ」の色を変えるだけで、私はなぜそれほど正確ではないのですか?Swift 3.0スピーチからテキストへ:単語の色を変更する
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