views:

115

answers:

2

A UILabel's text is drawn in a CGRect(x,y,size,width). The label can have multiple lines of text.

Is it possible to write a function to return a CGPoint within this rectangle that designates the position of the last character?

I ask because I am trying to wrap the text of a UILabel in an arbitrary prefix and suffix. The prefix/suffix need to be separate labels so I can set their font, color, etc. It's easy to find the start of the text, but finding the end of the text is tricky, to me at least.

+2  A: 

Perhaps a UIWebView will do the trick. Populate it with [webview LoadHTMLString:@"<b>prefix/b>text<b>suffix</b>" baseURL:nil]. This is a bit of a sledgehammer solution, but you obviously get lots of other goodies thrown into the bargain.

Marcelo Cantos
Thanks for thinking outside of my box.
Andrew Johnson
A: 

The main way to measure text on the iPhone is using NSString's sizeWithFont:forWidth:lineBreakMode. It is possible to build on this to do what you want, but it would be a bit of work. First, measure the prefix using your specified text traits and remember the dimensions. Then, calculate word breaks and incrementally add the measured widths for each word, line wrapping as necessary. Finally, measure your suffix and draw everything in the rectangles you've calculated.

sbooth