iOS自动调整标签高度

我使用下面的代码来自动调整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; 

在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;