Skip to content

Instantly share code, notes, and snippets.

@bouchtaoui-dev
Forked from Galeas/gist:7430979
Last active August 29, 2015 14:25
Show Gist options
  • Save bouchtaoui-dev/d52b6b8b797047b832ba to your computer and use it in GitHub Desktop.
Save bouchtaoui-dev/d52b6b8b797047b832ba 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