//---calculate the height needed to display the text
// based on given width, font, and font size---
-(CGFloat) textHeight:(NSString *) text withWidth:(int) viewWidth andSize:(int) fontSize{
CGSize maximumTextSize = CGSizeMake(viewWidth,9999);
CGSize expectedTextSize = [text sizeWithFont:[UIFont systemFontOfSize: fontSize]
constrainedToSize:maximumTextSize
lineBreakMode:NSLineBreakByWordWrapping];
return expectedTextSize.height;
}
To use the textHeight:withWidth:andSize: method, simply call it like this:
float textHeight = [self textHeight:@"The quick brown fox jumps over the lazy dog"
withWidth:300
andSize:15];
Have fun!
No comments:
Post a Comment