获取UILabel iOS8中的行数

我看到很多这个问题的弃用答案:

如何根据设置的文本计算UILabel中使用的行数?

我知道我需要将UILabel设置为使用换行调整大小的边界。 以这种方式,我可以检测到我的UILabel的高度,并调整一个NSLayoutConstraint来获得我的UITableViewCell的高度。 基本上我的问题简单而简单:

如何确定我的UILabel使用的行数(基于descriptionLabel.text )或高度,以调整包含我的UILabelUITableViewUITableViewCell的大小。

目前我已经使用这个代码:

descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 30, 270, 65)];
descriptionLabel.textColor = [UIColor blackColor];
descriptionLabel.numberOfLines = 0;
descriptionLabel.adjustsFontSizeToFitWidth = YES;

尝试使用下面的代码

NSAttributedString *attrStr = ... // your attributed string
CGFloat width = 300; // whatever your desired width is
CGRect rect = [attrStr boundingRectWithSize:CGSizeMake(width, 10000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];
//rect is the height of UILABEL which u can used as height of label or table view row or etc

我用这段代码很简单地解决了我的问题:

CGSize maxSize = CGSizeMake(290.0f, CGFLOAT_MAX);
CGSize requiredSize = [descriptionLabel sizeThatFits:maxSize];
[descriptionLabel setFrame:CGRectMake(15, 50, requiredSize.width, requiredSize.height)];
NSLog(@"THE HEIGHT OF THIS DESCRIPTIONLABEL IS: %f",cell.frame.size.height);
链接地址: http://www.djcxy.com/p/28207.html

上一篇: Get the number of lines in UILabel iOS8

下一篇: Adjust UILabel height to text