- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个 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/
在 macOS 上的 Swift 中,我有以下 TextView 并想设置输入到其中的文本的字体大小,如下所示: myTextView.typingAttributes = ["Unknown" :
我有 UITextView,我想将它的行高设置为 50.0f,所以我正在使用 typingAttributes,但没有任何效果,我的代码在 ViewDidAppear 方法中是这样的 UITextVi
我有一个 TextView ,其中填充了一些蓝色的默认文本,现在我的目标是,无论我在文本中进行什么编辑,它都应该显示为红色。我正在使用 texView.typingAttributes 来实现这个并且
我使用 typingAttributes 设置新字体。在 iOS 10 上,一切正常,但在 iOS 11 上,第一个输入的字符是正确的,但随后属性被重置为以前的,第二个字符被输入以前的字体。这是一个错
我正在尝试将 UITextfield 中键入的文本颜色更改为白色。我使用了以下代码,该代码有效但不会将第一个字母的颜色更改为白色。请参阅随附的屏幕截图。 如何使第一个字母的颜色也变为白色? 我搜索了很
我正在使用 UITextView 输入一些富文本。我创建了一个按钮来更改段落对齐方式(左、中、右)。 当用户选择一些文本并且它按预期工作时,我将对齐应用于 attributedText。 但是,当用户
我正在使用以下方法在 Swift 3 中将我的 textView.typingAttributes 前景色设置为红色。 textView.typingAttributes[NSForegroundCo
我正在尝试在我的应用中创建一个 RichTextEditor。 用户在编辑时可以同时更改前景色和背景色。 这是我设置 typingAttributes 的方法: func textView(_ tex
这个问题在这里已经有了答案: Swift 4 attributedString get typing attributes (3 个答案) 关闭 5 年前。 我曾经能够生成 NSAttributed
我是一名优秀的程序员,十分优秀!