Skip to content

Instantly share code, notes, and snippets.

@maninp
Forked from Galeas/gist:7430979
Last active August 29, 2015 14:24
Show Gist options
  • Save maninp/e8f34d51befb6695918a to your computer and use it in GitHub Desktop.
Save maninp/e8f34d51befb6695918a to your computer and use it in GitHub Desktop.
NSString *str = <#some string#>;
CGSize size;
CGSize maxSize = CGSizeMake(<#width#>, <#height#>);
UIFont *font = <#font#>;
if ([str respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) {
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setLineBreakMode:<#Line break mode#>];
[style setAlignment:<#String alignment#>];
NSDictionary *attributes = @{ NSFontAttributeName:font,
NSParagraphStyleAttributeName:style
};
size = [str boundingRectWithSize:maxSize options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
}
else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
size = [str sizeWithFont:font constrainedToSize:maxSize lineBreakMode:<#Line break mode#>];
#pragma clang diagnostic pop
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment