Multi-colored UILabel text

风格不统一 提交于 2019-12-07 21:21:51

问题


Is is possible to create multi-colored text within a UILabel? I.e, if my text was:

"The quick brown fox"

have the q and b in blue with the rest of the text in black?

I get the feeling I'd have to use a UIWebView and render the text in HTML to accomplish this. Thoughts?


回答1:


You are correct that you need to use a UIWebView. Alternatively, you can draw the text yourself into a custom view, but that would probably be a lot more difficult to implement.

If you are going to draw it yourself, you'll want to use the drawing methods in NSString UIKit Additions along with the -[UIColor set] method etc.




回答2:


Rendering the text using HTML in a UIWebView is the recommended method from apple.




回答3:


Another solution is to concatenate an array of UILabels each with their own individual settings (such as text colour). In your case the label would be broken into labels as [The ][q][uick ][b][rown fox].




回答4:


CoreText can be successfully used in many applications.




回答5:


We can do it using CoreText Framework,You can study further more using this link CoreText Tutorial for iOS



来源:https://stackoverflow.com/questions/976799/multi-colored-uilabel-text

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!