Resize UILabel to Fit Font Size and Text


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

This code will resize a UILabel so that the label is as small as it can be in both width and height, based on the text in it and the font used.


Copy this code and paste it in your HTML
  1. frame = CGRectMake(160, labelTitle2.frame.origin.y + labelTitle2.frame.size.height + 136, 270, 25);
  2. UILabel *labelContent3 = [[UILabel alloc] initWithFrame: frame];
  3. labelContent3.text = @"click here.";
  4. labelContent3.textColor = UIColorFromRGB(0x606060);
  5. labelContent3.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:13];
  6. labelContent3.backgroundColor = [UIColor clearColor];
  7. labelContent3.textAlignment = UITextAlignmentLeft;
  8. labelContent3.numberOfLines = 0;
  9. maximumLabelSize = CGSizeMake(270,9999);
  10. expectedLabelSize = [labelContent3.text sizeWithFont:labelContent3.font
  11. constrainedToSize:maximumLabelSize
  12. lineBreakMode:labelContent3.lineBreakMode];
  13. newFrame = labelContent3.frame;
  14. newFrame.size.height = expectedLabelSize.height;
  15. labelContent3.frame = newFrame;
  16. [cell.contentView addSubview:labelContent3];
  17. [labelContent3 release];

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.