Skip to content

Instantly share code, notes, and snippets.

@mamaz
Last active August 29, 2015 14:06
Show Gist options
  • Save mamaz/74f2a31563804d744552 to your computer and use it in GitHub Desktop.
Save mamaz/74f2a31563804d744552 to your computer and use it in GitHub Desktop.
/*
Get UILabel's size for wordwrapping style
*/
+(CGSize)getLabelSizeWordWrapWithString:(NSString*)string
font:(UIFont*)font
constrainedToSize:(CGSize)constrainedSize
{
if (IOS_VERSION_LESS_THAN_7) {
CGSize size = [string sizeWithFont:font
constrainedToSize:constrainedSize
lineBreakMode:NSLineBreakByWordWrapping];
return size;
} else {
// for iOS7++
CGRect rect = [string boundingRectWithSize:constrainedSize
options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading
attributes:@{NSFontAttributeName: font}
context:nil];
return CGRectIntegral(rect).size;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment