简体   繁体   English

如何在Swift中的textView中突出显示文本?

[英]How to highlight text in a textView in Swift?

I would like to highlight a part of text in a UITextView like Pages for iOS or Safari for iOS do. 我想在UITextView中突出显示部分文本,例如Pages for iOS或Safari for iOS。 I search the documentation but I can't find a way to do it, maybe because I'm a beginner in iOS development and I miss some crucial info. 我搜索文档,但我找不到办法,也许是因为我是iOS开发的初学者,我错过了一些关键信息。 Somebody can help me? 有人可以帮帮我吗?

Screenshot of Pages for iOS: 适用于iOS的Pages屏幕截图:

适用于iOS的页面

Screenshot of Safari for iOS: Safari for iOS的屏幕截图:

适用于iOS的Safari

If you only need to highlight a single, contiguous block of text you can set the selection programmatically. 如果您只需要突出显示单个连续的文本块,则可以以编程方式设置选择。 Your examples show multiple discontinuous parts however. 您的示例显示了多个不连续的部分。 For that you are going to need to use an attributed string. 为此,您将需要使用属性字符串。 There is no other system-provided highlighting function. 没有其他系统提供的突出显示功能。 (There might be a third party library that offers this though - you'll have to search.) (可能会有第三方库提供此功能 - 您必须搜索。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM