Posted By

BenClayton on 04/21/10


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

umang_nine


iPhone: Resize a UILabel to fit the text inside it


 / Published in: Objective C
 

  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);
  6.  
  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  

You need to login to post a comment.