iosuilabelsizetofitsizewithfonttttattributedlabel

TTTAttributedLabel sizetofit and sizeWithFont is different


I am using TTTAttributedLabel and I usually use sizetofit in my tableview cell. After that, I use sizeWithFont in my cell height calculation. I set TTTAttributedLabel like this.

[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];

I notice that height is different. I got my output like this. So I guess it is wrong. May I know how to fix? I am testing on iOS 8 device.

enter image description here


Solution

  • You should calculate sizing instead with TTTAttributedLabel's built-in method

    +[TTTAttributedLabel sizeThatFitsAttributedString: withConstraints:limitedToNumberOfLines:, which will return a proper CGSize for you.