I want to find out how many lines a string will have when set to a font inside a rect. Is there a way to do this?
I have this code, but i’m trying to make it process that little bit faster, as it’s being used a lot in a scroll view:
CGSize maximumSize = CGSizeMake(MESSAGE_FRAME.size.width - 16, 1000000);
CGSize expectedSize = [message sizeWithFont:[UIFont fontWithName:@"Crimson" size:15]
constrainedToSize:maximumSize
lineBreakMode:UILineBreakModeWordWrap];
return expectedSize.height;
I know this returns line height:
NSLog(@"%f", [[UIFont fontWithName:@"Crimson" size:15] lineHeight]);
So if I can multiply that by another value, it might prove to be faster.
I can’t think of an alternative, and I doubt that there is one which will be much more efficient (after all, what possible magic could get to a line count and avoid the word wrap computation?).
Anyway, rather than hurt our brains shaving cycles off the computation, consider whether it needs to be done over and over while the view scrolls. My guess is no.
I think the way to speed this up is to do the traditional size computation and cache the result.