Posted By

suibhne on 01/10/11


Tagged

iphone objectivec


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

terababy
Tintenklecks
cnathan


Resize UILabel to Fit Font Size and Text


 / Published in: Objective C
 

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.

  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  

You need to login to post a comment.