Skip to content

Instantly share code, notes, and snippets.

@chrishulbert
Created April 28, 2011 04:59
Show Gist options
  • Save chrishulbert/945827 to your computer and use it in GitHub Desktop.
Save chrishulbert/945827 to your computer and use it in GitHub Desktop.
How to make a multiline label with dynamic text on the iphone and get the correct height
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 9999)];
myLabel.lineBreakMode = UILineBreakModeWordWrap;
myLabel.numberOfLines = 0;
myLabel.text = @"Some \n dynamic \n multiline \n text";
[myLabel sizeToFit]; // This shrinks the 9999 down to the size of the text
NSLog(@"Actual height is: %f", myLabel.frame.size.height); // Use this for spacing any further elements
[self.view addSubview:title]; // Or add it to a scroll view, or whatever...
[myLabel release];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment