iOS - 在 UITextView 中突出显示一个或多个单词 [英] iOS - Highlight One Word Or Multiple Words In A UITextView

查看:42
本文介绍了iOS - 在 UITextView 中突出显示一个或多个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我正在制作一小部分功能,允许用户通过一次突出显示一个单词来更快地阅读文本正文(在 UITextView 中),或者如果用户愿意,甚至可以有多个.UITextView 中的每个单词都应该按照用户设置的速度一个一个地突出显示.

In my project, I'm making a small piece of functionality that allows a user to read a body of text (in a UITextView) faster by having one word being highlighted at a time, or even multiple if a user wants. One by one, each word in the UITextView should be highlighted at the speed the user sets.

有什么办法可以在 UITextView 中突出显示单词的背景?我认为它与属性字符串有关,但我找不到任何突出显示 NSString 背景的内容.

Is there any way I could highlight the background of a word in a UITextView? I think it has something to do with attributed strings, but I can't find anything that highlights the background of an NSString.

任何帮助或建议将不胜感激.谢谢.

Any help or advice would be appreciated. Thanks.

推荐答案

它确实与属性字符串有关.它与 NSString 无关,因为属性字符串不是 NSString;它是一个 NSAttributedString,或者它的可变子类 NSMutableAttributedString.您要做的是将 NSBackgroundColorAttributeName 设置为所需单词范围的背景颜色.

It does have to do with attributed strings. It has nothing to do with NSString, because an attributed string is not an NSString; it is an NSAttributedString, or its mutable subclass NSMutableAttributedString. What you want to do is set the NSBackgroundColorAttributeName to a background color for the range of the desired word.

我还应该提到,您可以通过干预构建 UITextView 绘图系统的 Text Kit 堆栈来获得更时髦的效果,如下图所示,我在一个单词周围绘制了一个矩形:

I should mention also that you can obtain snazzier effects by intervening in the Text Kit stack that constructs the UITextView's drawing system, as in this image where I'm drawing a rectangle around a word:

但是,背景颜色应该足以让您入门.

However, the background color should be enough to get you started.

这篇关于iOS - 在 UITextView 中突出显示一个或多个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆