Images inside of UILabel
I'm trying to find the best way to display images inside of UILabels (in fact, I wouldn't mind switching to something other than UILabel if it supports images with no hassle)
The scenario is:
- I have a table view with hundreds of cells and UILabel being the main component of each cell
- The t开发者_开发百科ext I assign to each cell contains sequences of characters that need to be parsed out and represented as an image
In simpler words, imagine a TableView of an instant messenger that parses replaces all ":)", ":(", ":D" etc with corresponding smiley images
Any input would be greatly appreciated!
I believe that you can insert certain Unicode characters in your string, and the iPhone will display them as Emoji:
http://www.easyapns.com/category/just-for-fun
One approach would be to use a UIWebView instead of a UILabel. Replace each emoticon with an <img src=...>
tag where the image is in your bundle. Use loadHTMLString to populate the UIWebView and pass a URL for [[NSBundle mainBundle] resourcePath]
as the baseURL.
Another, more difficult approach to manually place images above the UILabel. Leave a few spaces where the emoticon would show up. Measure out the text and place a UIImageView above the UILabel for each emoticon.
You could also use CoreGraphics for all the text and image layout. Break the strings into text and emoticons. As you draw each string, the CGContext will update the text position where it will draw next. When you get to an emoticon, draw the appropriate image and advance the text position manually.
Using a UIWebView is probably the least difficult to code and has the added bonus of supporting styled text. It is also the most resource intensive unless you skip the table view and do everything in a single UIWebView. You can make it a little faster by using javascript to load content when reusing cells.
Using CoreGraphics is probably the fastest and least resource intensive if speed becomes an issue.
Maybe you could use UIWebView
s instead of UILabel
s
精彩评论