Skip to content

Instantly share code, notes, and snippets.

@icastell
Created October 10, 2012 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icastell/3866338 to your computer and use it in GitHub Desktop.
Save icastell/3866338 to your computer and use it in GitHub Desktop.
Adapt the UILabel height to its content text
- (void) adaptHeightView:(UILabel *)label withText:(NSString *)text
{
// First we must to set the text into the label
[label setText:text];
// Get the label frame with the entire text
CGRect frame = [label frame];
// Get the label size
// 1. Set the label font
// 2. Add size restrictions, set the label width and put a maximun height, in this case 9999
// 3. Establish the line break mode
// 4. Don't forget to set the number of lines to 0 in the UILabel
CGSize size = [label.text sizeWithFont:label.font
constrainedToSize:CGSizeMake(frame.size.width, 9999)
lineBreakMode:UILineBreakModeWordWrap];
// Finally, establish the new height to the label frame
frame.size.height = size.height;
[label setFrame:frame];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment