How do you return a multiline text CGSize from the new iOS 7 method sizeWithAttributes?
I would like this to produce the same results as sizeWithFont:constrainedToSi
well you can try this :
NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:14]};
// NSString class method: boundingRectWithSize:options:attributes:context is
// available only on ios7.0 sdk.
CGRect rect = [textToMeasure boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
Here's my method to deal with both situations, goes in an NSString
category.
- (CGSize) sizeWithFontOrAttributes:(UIFont *) font {
if (IS_IOS7) {
NSDictionary *fontWithAttributes = @{NSFontAttributeName:font};
return [self sizeWithAttributes:fontWithAttributes];
} else {
return [self sizeWithFont:font];
}
}
As an alternative, if you're looking at UITextView
, you can always use the NSLayoutManager
method:
CGSize textSize = [textView.layoutManager usedRectForTextContainer:textView.textContainer].size;
You can also find the line height for a given font by:
UIFont *font;
CGFloat lineHeight = font.lineHeight;