I need a method like CTFrameGetVisibleStringRange that can give me the text that will be rendered in a given size supplied with a line break mode (i.e. word wrap). For example I have a long line of text.. and I have a given rectangle to draw the text wrapped in it, but wherever the text gets trunecated, I continue rendering it in another area where it left off. So I need a method like:
NSString * text = "The lazy fox jumped over the creek";
[text drawAtPoint:CGPointMake(0, 0) forWidth:20 withFont:[UIFont fontWithName:@"Arial" size:10] lineBreakMode:UILineBreakModeWordWrap];
// now I do I know how much it drew before it stopped rendering?
Anyone have any ideas?
**EDITED: Please see my solution.
I had a similar problem, and I used the solution Mike posted.
It turned out, however, that
trimToWord
was often giving me a few too many words than could fit on my UILabel size specified. I found out that if I changed the while loop operator to a >= and not just a >, it worked perfectly.I also added a few ivars(
chopIndex
andremainingBody
) that I used to get the remaining string so I could display it in my next UILabel.Here's the solution I used.
Here is a solution. It is fairly fast. It 'guesses' where to chop first and then rolls back word by word. sizewithFont calls are fairly expensive so this intial 'guess' step is important. The main method is trimToWord: sizeConstraints:withFont.
Feel free to comment on how I could improve this.
I do'nt think,there is subsitute for
CTFrameGetVisibleStringRange
, Although we can get the same with the use of the below method.Apple Documentation
http://developer.apple.com/library/ios/#documentation/uikit/reference/NSString_UIKit_Additions/Reference/Reference.html
EDITED: The below code show my approach