UITextField 中的彩色子字符串
我想在用户在 UITextField
中输入时仅更改特定子字符串(关键字)的颜色。
在文档中,我看到了属性 textColor
但它确实改变了整个文本字符串的颜色,而我只想突出显示一些关键字。
是否可以?
I would like to change the color only of particular substrings (keywords) while the user is typing in a UITextField
.
In the documentation I saw the property textColor
but it does change the color of the entire text string, while I would like to highlight only some keywords.
Is it possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简短的回答是否定的。
如果您使用 NSAttributedStrings 和 CATextLayers 或 Core Text 从头开始创建自己的文本字段,这是可能的,但这是一个极其复杂和困难的问题。
The short answer is no.
It's possible if you create your own textfield from scratch using NSAttributedStrings and CATextLayers or Core Text, but this is an incredibly complex and difficult problem.
如果您要镜像用户在某种预览框中键入的内容,则可以创建多个彼此相邻且具有不同颜色或字体等的标签或文本字段。否则,恐怕尼克的答案是准确的 - 在单个文本字段中它不会发生。
祝你好运,
达米安
If you are mirroring what the user types in a preview box of some sort it is possible to create multiple labels or text fields that stay next to each other and have different colors or fonts or whatever. Otherwise, I'm afraid Nick's answer is accurate - within a single textField it ain't happening.
Good luck,
Damien