跟踪和更改 UITextView 中文本的颜色
Keeping track of and changing color of text in UITextView
当用户在文本视图中输入内容时,我正在查看每个单词并查看它是否与我拥有的数组中的单词相匹配。如果匹配,则该词变为蓝色,并且将布尔变量 didFindACertainWord
设置为 true(以确保只有一个词为蓝色)。我能够成功完成这部分,但出现了一些错误:
我改成蓝色的某个词可以用,但是字体之前输入的词被改变了,我在这个词之后输入的任何东西也是蓝色的(我不想要)。 我只想把某个字改成蓝色,其他字保持黑色和原来的字体。
我不知道如何查明用户是否删除了某个单词。如果他们这样做,我想将特定单词的颜色改回黑色(在他们删除特定单词的第一个字符之后)并将 didFindACertainWord
设置为 false。
这是我的 textViewDidChange
方法中的当前代码:
func textViewDidChange(_ textView: UITextView) {
//get last word typed
let size = textView.text.reversed().firstIndex(of: " ") ?? textView.text.count
let startWord = textView.text.index(textView.text.endIndex, offsetBy: -size)
let lastWord = textView.text[startWord...]
//check if last word is in array and we did not already find one
if certainWords.contains(String(lastWord)) && !didFindACertainWord {
didFindACertainWord = true
//change color of the word
let attributedString = NSMutableAttributedString.init(string: textView.text)
let range = (textView.text as NSString).range(of: String(lastWord))
attributedString.addAttributes([NSAttributedString.Key.foregroundColor: UIColor.blue, NSAttributedString.Key.font: UIFont(name: "Avenir-Roman", size: 18)], range: range)
textView.attributedText = attributedString
}
}
我错过了什么/我怎样才能成功做到这一点? P.S。文本视图中所有文本的字体应为 UIFont(name: "Avenir-Roman", size: 18)
我正在搜索每个词,因为在用户键入一个动作词后,如果它们与动作词相关,我需要阅读下一个词以将它们加粗。例如,如果用户键入 "see Paris London Berlin to find the best food",操作词是 "see",要加粗的相关词是 "Paris Italy France",不相关的词(将采用常规字体)是 "to find the best"
第一个问题,因为你应该做一个"else"案例,重新设置颜色和布尔值。
您应该添加:
} else {
didFindACertainWord = false
textView.attributedText = attributedString
}
对于第二个,您不需要只处理最后一个单词,而是检查整个字符串是否匹配。
未测试,但应该可以工作:
func textViewDidChange(_ textView: UITextView) {
let attributedString = NSMutableAttributedString(string: textView.text,
attributes: [.font: UIFont(name: "Avenir-Roman", size: 18)])
let allWords = attributedString.string.components(separatedBy: CharacterSet.whitespaces)
if let firstMatch = allWords.first(where: { return certainWords.contains([=11=])}) {
didFindACertainWord = true
let firstMatchRange = (attributedString.string as NSString).range(of: firstMatch)
attributedString.addAttribute(.foregroundColor, value: UIColor.blue, range: firstMatchRange)
} else {
didFindACertainWord = false
}
textView.attributedText = attributedString
}
当用户在文本视图中输入内容时,我正在查看每个单词并查看它是否与我拥有的数组中的单词相匹配。如果匹配,则该词变为蓝色,并且将布尔变量 didFindACertainWord
设置为 true(以确保只有一个词为蓝色)。我能够成功完成这部分,但出现了一些错误:
我改成蓝色的某个词可以用,但是字体之前输入的词被改变了,我在这个词之后输入的任何东西也是蓝色的(我不想要)。 我只想把某个字改成蓝色,其他字保持黑色和原来的字体。
我不知道如何查明用户是否删除了某个单词。如果他们这样做,我想将特定单词的颜色改回黑色(在他们删除特定单词的第一个字符之后)并将
didFindACertainWord
设置为 false。
这是我的 textViewDidChange
方法中的当前代码:
func textViewDidChange(_ textView: UITextView) {
//get last word typed
let size = textView.text.reversed().firstIndex(of: " ") ?? textView.text.count
let startWord = textView.text.index(textView.text.endIndex, offsetBy: -size)
let lastWord = textView.text[startWord...]
//check if last word is in array and we did not already find one
if certainWords.contains(String(lastWord)) && !didFindACertainWord {
didFindACertainWord = true
//change color of the word
let attributedString = NSMutableAttributedString.init(string: textView.text)
let range = (textView.text as NSString).range(of: String(lastWord))
attributedString.addAttributes([NSAttributedString.Key.foregroundColor: UIColor.blue, NSAttributedString.Key.font: UIFont(name: "Avenir-Roman", size: 18)], range: range)
textView.attributedText = attributedString
}
}
我错过了什么/我怎样才能成功做到这一点? P.S。文本视图中所有文本的字体应为 UIFont(name: "Avenir-Roman", size: 18)
我正在搜索每个词,因为在用户键入一个动作词后,如果它们与动作词相关,我需要阅读下一个词以将它们加粗。例如,如果用户键入 "see Paris London Berlin to find the best food",操作词是 "see",要加粗的相关词是 "Paris Italy France",不相关的词(将采用常规字体)是 "to find the best"
第一个问题,因为你应该做一个"else"案例,重新设置颜色和布尔值。 您应该添加:
} else {
didFindACertainWord = false
textView.attributedText = attributedString
}
对于第二个,您不需要只处理最后一个单词,而是检查整个字符串是否匹配。
未测试,但应该可以工作:
func textViewDidChange(_ textView: UITextView) {
let attributedString = NSMutableAttributedString(string: textView.text,
attributes: [.font: UIFont(name: "Avenir-Roman", size: 18)])
let allWords = attributedString.string.components(separatedBy: CharacterSet.whitespaces)
if let firstMatch = allWords.first(where: { return certainWords.contains([=11=])}) {
didFindACertainWord = true
let firstMatchRange = (attributedString.string as NSString).range(of: firstMatch)
attributedString.addAttribute(.foregroundColor, value: UIColor.blue, range: firstMatchRange)
} else {
didFindACertainWord = false
}
textView.attributedText = attributedString
}