获取UILabel iOS8中的行数
我看到很多这个问题的弃用答案:
如何根据设置的文本计算UILabel
中使用的行数?
我知道我需要将UILabel
设置为使用换行调整大小的边界。 以这种方式,我可以检测到我的UILabel
的高度,并调整一个NSLayoutConstraint
来获得我的UITableViewCell
的高度。 基本上我的问题简单而简单:
如何确定我的UILabel使用的行数(基于descriptionLabel.text
)或高度,以调整包含我的UILabel的UITableView的UITableViewCell的大小。
目前我已经使用这个代码:
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