问题表现
如图,label 超出了cell的范围,明显的,计算出来的文字高度没有
label真实高度大
代码
添加约束使用masonry, 计算高度使用的是frame
///label 约束
[self.answerContentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(57 * rectScale());
make.top.mas_equalTo(52 * rectScale());
make.width.mas_equalTo(ScreenWidth - 72 * rectScale() - 33 * rectScale())
;
}];
//计算高度
[self.answerContentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(57 * rectScale());
make.top.mas_equalTo(52 * rectScale());
make.width.mas_equalTo(ScreenWidth - 72 * rectScale() - 33 * rectScale())
;
}];
检查
我们发现
如果修改成如下使用frame 添加布局,就会没有问题
_answerContentLabel = [[UILabel alloc] initWithFrame:CGRectMake(57 * rectScale(), 52 * rectScale(), ScreenWidth - 72 * rectScale() - 33 * rectScale(), 0)];
使用图层分别查看label的 信息,发现虽然使用masonry 和 frame
设置的宽度是同样的值,但是真实绘制的label 宽度并不想等
在这里插入图片描述
启示
如果我们使用sizeThatFits的方式计算label内容的高度,则我们的label
布局的时候也要使用frame,否则可能会因为使用masonry布局导致
实际绘制的宽度和计算高度使用的宽度并不想等