Skip to content

Instantly share code, notes, and snippets.

@ialex32x
Created August 28, 2017 08:08
Show Gist options
  • Save ialex32x/343f0d84828b067effa2588e519cbda7 to your computer and use it in GitHub Desktop.
Save ialex32x/343f0d84828b067effa2588e519cbda7 to your computer and use it in GitHub Desktop.
get size of string with font in cocoa touch
NSString *str = ...;
UIFont *font = [UIFont systemFontOfSize:12.0f];
NSDictionary<NSString *, id> *textAttributes = @{NSFontAttributeName: font};
CGRect rect = [str boundingRectWithSize:CGSizeMake(self.bounds.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:textAttributes context:nil];
return rect.size.height;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment