iPhone: Resize a UILabel to fit the text inside it

/ Published in: Objective C
Save to your folder(s)

Copy this code and paste it in your HTML
  1. // resize description label to fit text..
  2. UIFont* font = descriptionLabel.font;
  3. CGSize constraintSize = CGSizeMake(descriptionLabel.frame.size.width, MAXFLOAT);
  4. CGSize labelSize = [descriptionLabel.text sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
  5. descriptionLabel.frame = CGRectMake(descriptionLabel.frame.origin.x, descriptionLabel.frame.origin.y, descriptionLabel.frame.size.width, labelSize.height);
  7. // if the label's in a UIScrollView, and the label is at the bottom, here's how
  8. // to make it scroll correctly.
  9. // make the size of the scroll view be the distance from the top to the bottom of the
  10. // description label.
  11. scrollView.contentSize = CGSizeMake(scrollView.contentSize.width, descriptionLabel.frame.origin.y + descriptionLabel.frame.size.height);

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.