Skip to content

Instantly share code, notes, and snippets.

@maninp
Forked from xuyunan/gist:c72626bca1cd05731aae
Last active August 29, 2015 14:24
Show Gist options
  • Save maninp/c6ada5379e9c404c475c to your computer and use it in GitHub Desktop.
Save maninp/c6ada5379e9c404c475c to your computer and use it in GitHub Desktop.
- (CGFloat)heighWithFont:(UIFont *)font constrainedToSize:(CGSize)size
{
return [self sizeWithFont:font constrainedToSize:size].height;
}
- (CGFloat)widthWithFont:(UIFont *)font constrainedToSize:(CGSize)size
{
return [self sizeWithFont:font constrainedToSize:size].width;
}
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
{
CGRect stringRect;
if ([self respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) {
stringRect = [self boundingRectWithSize:size
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:@{NSFontAttributeName: font}
context:NULL];
} else {
CGSize stringSize = [self sizeWithFont:font constrainedToSize:size];
stringRect = CGRectMake(0, 0, stringSize.width, stringSize.height);
}
return stringRect.size;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment