TTTAttributedLabel sizetofit和sizeWithFont是不同的

Kha*_*inn 1 uilabel sizewithfont ios sizetofit 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];
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

Jon*_*rsh 5

您应该使用TTTAttributedLabel内置方法计算大小

+[TTTAttributedLabel sizeThatFitsAttributedString: withConstraints:limitedToNumberOfLines:,这将为CGSize你返回一个适当的.