Skip to content

Instantly share code, notes, and snippets.

@xtreme-daniel-crampton
Created March 25, 2014 21:40
Show Gist options
  • Save xtreme-daniel-crampton/9771969 to your computer and use it in GitHub Desktop.
Save xtreme-daniel-crampton/9771969 to your computer and use it in GitHub Desktop.
Returns a CGSize for a multi-line string.
- (CGSize)adjustedSizeWithFont:(UIFont *)font fromDefaultSize:(CGSize)defaultSize {
CGFloat additionalHeight = 0.0f;
NSDictionary *attributes = @{NSFontAttributeName : font};
CGRect rect = [self boundingRectWithSize:CGSizeMake(defaultSize.width, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
//Doesn't matter what the string is here. Just need to get the height of one line of text.
CGRect singleLineRect = [@"X" boundingRectWithSize:CGSizeMake(defaultSize.width, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
additionalHeight -= CGRectGetHeight(singleLineRect);
additionalHeight += CGRectGetHeight(rect);
return CGSizeMake(defaultSize.width, defaultSize.height + ceil(additionalHeight));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment