TTTAttributedLabel sizetofit和sizeWithFont是不同的

时间:2015-05-06 14:34:33

标签: ios uilabel sizetofit sizewithfont tttattributedlabel

我正在使用TTTAttributedLabel,我通常在tableview单元格中使用sizetofit。之后,我在单元格高度计算中使用sizeWithFont。我像这样设置了TTTAttributedLabel。

[self.attributedLabel setText:@"Test\n\n\n\n"];
CGSize contentSize = [self.attributedLabel.text sizeWithFont:self.attributedLabel.font
                                      constrainedToSize:CGSizeMake(CGRectGetWidth(self.attributedLabel.frame), 1000)
                                          lineBreakMode:NSLineBreakByWordWrapping];

[self.attributedLabel sizeToFit];

我注意到身高不同。我得到了这样的输出。所以我猜这是错的。我可以知道如何解决?我在iOS 8设备上进行测试。

enter image description here

1 个答案:

答案 0 :(得分:5)

您应该使用TTTAttributedLabel的内置方法来计算大小调整

+[TTTAttributedLabel sizeThatFitsAttributedString: withConstraints:limitedToNumberOfLines:,会为您返回正确的CGSize

相关问题