Created
October 10, 2012 15:25
-
-
Save icastell/3866338 to your computer and use it in GitHub Desktop.
Adapt the UILabel height to its content text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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