Skip to content

Instantly share code, notes, and snippets.

@darkseed
Forked from chrishulbert/multi line label.m
Created August 20, 2011 20:05
Show Gist options
  • Save darkseed/1159594 to your computer and use it in GitHub Desktop.
Save darkseed/1159594 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