iOS自动调整标签高度

时间:2013-12-16 18:35:51

标签: ios uitableview uilabel cgrect

我正在使用以下代码自动调整UITableView中标签的高度。它在大多数时间都有效,但某些时候文本被截断。我的代码或其他任何我需要添加的内容是否有问题?

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;

1 个答案:

答案 0 :(得分:1)

在iOS 7 sizeWithFont: constrainedToSize: lineBreakMode:已弃用,现在您应该使用:

 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;