Skip to content

Instantly share code, notes, and snippets.

@Lejdborg
Created November 27, 2012 06:43
Show Gist options
  • Save Lejdborg/4152775 to your computer and use it in GitHub Desktop.
Save Lejdborg/4152775 to your computer and use it in GitHub Desktop.
- [UILabel resizeToFit:]
/**
* Resizes a UILabel to be exactly the size it needs to be to fit it's contents
* within the constraints of CGSize.
*
* @param maxSize The maximum allowable size the label may fit in
*/
- (void)resizeToFit:(CGSize)maxSize {
CGSize expectedLabelSize = [[self text] sizeWithFont:[self font]
constrainedToSize:maxSize
lineBreakMode:[self lineBreakMode]];
CGRect newFrame = [self frame];
newFrame.size = expectedLabelSize;
[self setFrame:newFrame];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment