swalkner
swalkner

Reputation: 17339

AutoLayout + heightForRowAtIndexPath/sizeWithFont not working correctly

I've got a custom cell with the following AutoLayout:

    NSMutableArray *constraints = [NSMutableArray new];
    [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[descriptionLabel]-35-|" options:0 metrics:nil views:views]];
    [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[descriptionLabel]-10-|" options:0 metrics:nil views:views]];
    [self.contentView addConstraints:constraints];

In my tableViewDelegate, the heightForRowAtIndexPath: is calculated like this:

return [cell.descriptionLabel systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height + 20;

Unfortunately, it doesn't work when my text wraps - it's only correct for cells with text where no wrapping is happening. What am I missing?

Upvotes: 1

Views: 545

Answers (1)

hybridcattt
hybridcattt

Reputation: 3041

When I first faced this issue in iOS7 I've tried all possible ways to correctly estimate size of a wrapped or attributed text.

And it appeared that the only accurate way is sizeThatFits:

label.text = @"...";
CGSize s = [label sizeThatFits:CGSizeMake(label.frame.size.width, CGFLOAT_MAX)];

Upvotes: 2

Related Questions