views:

163

answers:

4

I need to format text in a label like this:

username: some text from this user. This will
create additional lines of text that will go
on and on and on.

Where "username" is bold. This will go into a UILabel, which is in a custom table cell. Is there a way to get this type of layout?

+1  A: 

If you use plain UILabel it's not available. Use two labels for this task.

buratinas
How do I get the wrapping under the username label with two labels?
4thSpace
A: 

You need to use either a UIWebView or CoreText to do this kind of advanced text layout. A web view has a lot of overhead but is most flexible and you can't use it effectively in a UITableView cell. CoreText is low level and not that well documented. You could ditch the table view and just lay out the table with CSS and HTML in the web view, which is how I do it.

lucius
A: 

You can still use a UITableViewCell but have the cell use a UIWebView subview. Set up a custom cell subclass with a clever setter method that allows you to send nsstrings to the method with turns those into a pretty formatted view.

Cirrostratus
+1  A: 

For this relatively simple case, you might be able to fake it. Have one label with the bold username, and another label with the plain text in the same position. Insert enough spaces before the plain text to leave room for the username. You can use UIStringDrawing methods to measure the bold text and the spaces.

CGSize usernameSize = [theUsername sizeWithFont:theBoldUsernameFont];
CGSize spaceSize = [@" " sizeWithFont:thePlainCommentFont];
NSString *indentedComment = [NSString stringWithFormat:@"%*s%@" , (int)ceil( usernameSize.width / spaceSize.width ) , "" , theComment];
drawnonward
You know, just before you posted that, that's exactly what I started doing. It's just a matter of calculating the number of spaces off the user name text.
4thSpace