views:

1144

answers:

1

This is an iPhone question. How does one draw wrappped text in the drawRect method? I'm trying to use atebits' ABTableViewCell class to do custom drawing of a table view cell. I'd like to draw a small picture and then text next to it which may or may not need to line wrap. Think of the table cells in a Twitter client like Tweetie which show a profile picture and a tweet. How is that done?

I tried using the various NSString drawAtPoint methods, but they don't seem to wrap the text into multiple lines.

+2  A: 

I think you need one of the drawInRect:withFont: methods. They are documented as performing word wrapping. If you think about it, it makes sense that drawAtPoint wouldn't work, because it doesn't know the rectangle the text needs to fit into.

n8gray
Thank you, n8gray. It looks like drawInRect:withFont:lineBreakMode:alignment: does what I'm after. For some reason, a number of the drawAtPoint methods also take forWidth and lineBreakMode arguments which is why I tried those. Actually, some of the drawInRect methods (e.g. drawInRect:withFont:lineBreakMode:) also appear to render text in a single line despite the impression you might get from the name. This threw me off, but I guess I hadn't tried them all. Anyway, you just made my Saturday.
sam
lineBreakMode also controls truncation (head/tail) so it's appropriate for single-line rendering as well.
porneL