views:

170

answers:

2

In the iPhone Address Book app, the last names/companies are all in bold text. I'd like to do some simple text formatting within a cell.

I've seen some mentions of using UIWebView, but this seems like a very heavy solution. Additionally, it's complicated because one has to either fix the cell size or do some special handling to get the size back.

The other alternative that I know of is to render the cell manually. This might not be too hard for simple things, but obviously can get complicated very quickly.

Has any one found a simply way to get basic text formatting in a UITableView?

A: 

Seems like there are two questions you might be asking:

  1. How can you change the formatting of items in a table? Change the properties of the textLabel and detailLabel in your cells. Nothing to it.

  2. How can you create items that have mixed formatting (e.g. the mixed plain and bold text in the Contacts app)? Labels don't do that, so you'd have to do this yourself. Either subclassing UILabel to override how it draws the text, or perhaps doing the drawing directly in -[UITableViewCell drawRect:].

Sixten Otto
I'm not asking how to change the formatting of the entire text label. I specifically mentioned the address book because it has mixed formatting.
brianegge
OK, and that's why I covered both alternatives. But your question wasn't very clearly worded, and both are reasonable interpretations.
Sixten Otto
+1  A: 

The general answer is to render the cell manually. Yes, it gets complicated quickly, but it is how you generally do it. If your needs are extremely simple, you can get away with creating separate UILabels for each piece, and then format each of them and then lay them out, but in most cases this quickly becomes as complicated as just rendering the text yourself, and is almost always slower.

NSAttributedString and related UIKit drawing extensions would be a welcome addition to iPhone, but it isn't available. It basically comes down to hand rendering, or use a web view.

Rob Napier