0

I'm using the following code to auto adjust the height of a label in a UITableView. It works the majority of the time, but certain times text is cut off. Is there something wrong with my code, or anything else I need to add? (code based on this answer)

UILabel *textLabel = ((UILabel *)[cell viewWithTag:3]);
textLabel.text = text;

CGSize maximumLabelSize = CGSizeMake(296, FLT_MAX);

CGSize expectedLabelSize = [text sizeWithFont:textLabel.font constrainedToSize:maximumLabelSize lineBreakMode:textLabel.lineBreakMode];

//adjust the label the the new height.
CGRect newFrame = textLabel.frame;
newFrame.size.height = expectedLabelSize.height;
textLabel.frame = newFrame;
Ryan M
  • 18,333
  • 31
  • 67
  • 74
Jhorra
  • 6,233
  • 21
  • 69
  • 123

1 Answers1

1

In iOS 7 sizeWithFont: constrainedToSize: lineBreakMode: is deprecated, now you should use:

 CGSize maxSize = CGSizeMake(296.f, FLT_MAX);
 CGRect labRect = [someText boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:textLabel.font} context:nil];


 textLabel.frame = CGRectMake(0, 0, maxSize.width, labRect.size.height);
 textLabel.text = someText;
Lennart
  • 9,657
  • 16
  • 68
  • 84
Ilario
  • 5,979
  • 2
  • 32
  • 46
  • I tried your solution and it wants to know the value of width. It also wanted x and y, but I'm assuming they're 0 and 0. For width would it just be the width of the screen? – Jhorra Dec 16 '13 at 21:30
  • @Jhorra yes width is width of the screen or less.. for example 296 as maxSize width – Ilario Dec 16 '13 at 21:35