textView.typingAttributes 不适用于预测
textView.typingAttributes not working for prediction
我有一个文本视图,其中填充了一些蓝色的默认文本,现在我的目标是,无论我在文本中进行什么编辑,它都应该是红色的。我正在使用 texView.typingAttributes
来实现这一点并且效果几乎很好,除非我从任何蓝色单词开始使用预测或自动更正,在这种情况下,预测或自动更正的单词仍然保持蓝色。如果我在蓝色区域之外使用预测,它工作正常。我还应该为预测做些什么吗?下面是我当前用于实现此目的的代码。
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
textView.typingAttributes[NSForegroundColorAttributeName] = UIColor.red
return true
}
为了更正,您需要为选定的字符串范围应用颜色属性 属性。现在可以使用了,添加了如下代码:
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
textView.typingAttributes[NSForegroundColorAttributeName] = UIColor.red
let colorAttribute = [NSForegroundColorAttributeName: UIColor.red]
textView.textStorage.addAttributes(colorAttribute, range: range)
return true
}
我有一个文本视图,其中填充了一些蓝色的默认文本,现在我的目标是,无论我在文本中进行什么编辑,它都应该是红色的。我正在使用 texView.typingAttributes
来实现这一点并且效果几乎很好,除非我从任何蓝色单词开始使用预测或自动更正,在这种情况下,预测或自动更正的单词仍然保持蓝色。如果我在蓝色区域之外使用预测,它工作正常。我还应该为预测做些什么吗?下面是我当前用于实现此目的的代码。
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
textView.typingAttributes[NSForegroundColorAttributeName] = UIColor.red
return true
}
为了更正,您需要为选定的字符串范围应用颜色属性 属性。现在可以使用了,添加了如下代码:
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
textView.typingAttributes[NSForegroundColorAttributeName] = UIColor.red
let colorAttribute = [NSForegroundColorAttributeName: UIColor.red]
textView.textStorage.addAttributes(colorAttribute, range: range)
return true
}