views:

221

answers:

2

hello everyone!

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 text 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!

A: 

Maybe you could use UIWebViews instead of UILabels

Thomas Müller
+2  A: 

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.

drawnonward
Thank you, I will probably use a combination of both methods, UIWebView will be great for tables with static number of cells, and CGContext better for long lists. I wish they had something like a rich text formatter, without all the heaviness of UIWebView
Nick
by the way, this is a reason why UIWebView won't work for large lists: http://stackoverflow.com/questions/646809/how-do-i-use-a-uiwebview-in-a-table-cell
Nick