gpt4 book ai didi

ios - textView.typingAttributes 不适用于预测

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:31:06 25 4
gpt4 key购买 nike

我有一个 TextView ,其中填充了一些蓝色的默认文本,现在我的目标是,无论我在文本中进行什么编辑,它都应该显示为红色。我正在使用 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
}

关于ios - textView.typingAttributes 不适用于预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45276584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com