views:

621

answers:

2

Trying to implement a UITableView of names similar to the built-in Contacts iPhone app where the first name is in normal font and the last name is bold. A quick google suggests this isn't as easy as it sounds due to the iPhone SDKs lack of rich text support in UILabels.

One of the solutions found recommended using a UIWebView to format the text allowing HTML bold tags to provide the required markup. Correct me if I'm wrong but this seems to be overkill and would have an affect on the performance of the app.

Another solution which I'm trying to investigate more is to create a custom UITableViewCell containing two UILabels side by side with the second label formatted for bold text. This seems straightforward, but I'm not sure how to have the labels auto-size horizontally to fit the text (up to a certain max width). Is this possible via the Interface Builder or does this need to be done programmatically?

Am I on the right track here or is there a much easier way to achieve this seemingly trivial effect?

+1  A: 

I wonder if you should subclass UILabel and do the drawing yourself. Then you could use CGContextSelectFont, CGContextGetTextPosition, CGContextSetTextPosition, CGContextShowText etc to draw your text.

Select the normal font, set the initial text position, draw your text, advance the text position by a few pixels, select the bold font, draw your bold text, and so on.

I haven't done this myself before, but am thinking of using this solution for one of my applications as well.

Thomas Müller
This is how Apple does it (though I think they use the higher-level `UIStringDrawing` methods)
rpetrich
I ended up doing just this (with the UIStringDrawing methods) and rolled my own lightweight RichTextLabel allowing bold, italic, and underlined text to be defined using HTML markup. The TTStyledTextLabel solution I mentioned below was too tightly coupled to the Three20 framework which was overkill for what I required.
Andy Shea
A: 

It looks like this has been asked a few times on Stack Overflow under various guises. A common solution seems to be the Three20 library's TTStyledTextLabel. Until the iPhone SDK includes an inbuilt rich text label and short of rolling my own, this class looks to be the next best thing.

Andy Shea