I'm developing an application for the iPhone where some text is shown in a UITextView. I want to search for a specific word in the text and then show the text with that specific word colored. If anyone can help me solve this, I'll be very glad.
我正在为iPhone开发一个应用程序,其中一些文本显示在UITextView中。我想在文本中搜索特定的单词,然后显示具有该特定单词的文本。如果有人能帮我解决这个问题,我会很高兴的。
1 个解决方案
#1
UITextView does not allow any formatting of the text within it. If the text is static (i.e. doesn't need to be edited), use a UIWebView instead; that will allow you to apply HTML formatting.
UITextView不允许对其中的文本进行任何格式化。如果文本是静态的(即不需要编辑),请改用UIWebView;这将允许您应用HTML格式。
If a web view isn't an option, your best bet will be to try to calculate where the text in question will fall in the text view and then draw some sort of highlight around it. NSString has some methods to calculate how much space you need to draw a given string on screen; by being very, very clever with those methods, you may be able to work out where the word you're trying to highlight is. Unfortunately, this will not be at all simple.
如果网页视图不是一个选项,那么您最好的选择是尝试计算文本视图中文本的位置,然后在其周围绘制某种突出显示。 NSString有一些方法可以计算在屏幕上绘制给定字符串所需的空间大小;通过非常非常聪明地使用这些方法,您可以找出您想要突出显示的单词的位置。不幸的是,这一点都不简单。
If you do come up with a solution, everyone here would probably love to hear about it!
如果你想出一个解决方案,这里的每个人都可能会喜欢听到它!
#1
UITextView does not allow any formatting of the text within it. If the text is static (i.e. doesn't need to be edited), use a UIWebView instead; that will allow you to apply HTML formatting.
UITextView不允许对其中的文本进行任何格式化。如果文本是静态的(即不需要编辑),请改用UIWebView;这将允许您应用HTML格式。
If a web view isn't an option, your best bet will be to try to calculate where the text in question will fall in the text view and then draw some sort of highlight around it. NSString has some methods to calculate how much space you need to draw a given string on screen; by being very, very clever with those methods, you may be able to work out where the word you're trying to highlight is. Unfortunately, this will not be at all simple.
如果网页视图不是一个选项,那么您最好的选择是尝试计算文本视图中文本的位置,然后在其周围绘制某种突出显示。 NSString有一些方法可以计算在屏幕上绘制给定字符串所需的空间大小;通过非常非常聪明地使用这些方法,您可以找出您想要突出显示的单词的位置。不幸的是,这一点都不简单。
If you do come up with a solution, everyone here would probably love to hear about it!
如果你想出一个解决方案,这里的每个人都可能会喜欢听到它!