使用多行标签时 systemLayoutSizeFittingSize 返回错误的大小

Wrong size returned by systemLayoutSizeFittingSize when using multiline label

我有一个自定义 table 单元格 MyCell,它只有 1 个多行标签,两侧都有约束。 xib 大小为 320 x 280

我正在使用 systemLayoutSizeFittingSize 根据内容计算单元格高度:

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        MyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];

        // set cell width = table width
        CGRect rect = cell.frame;
        rect.size.width = CGRectGetWidth(tableView.frame);
        cell .frame= rect;

        // update the text
        cell.mainLabel.text = @"Some multiline text. Some multiline text. Some multiline text. Some multiline text. Some multiline text. Some multiline text. Some multiline text. Some multiline text.";

        [cell setNeedsLayout];
        [cell layoutIfNeeded];

        CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];

        NSLog(@"size %@", NSStringFromCGSize(size)); // {290.5, 117.5}

        return size.height;
    }

MyCell layoutSubviews 看起来像这样

- (void) layoutSubviews {
    [super layoutSubviews];

    NSLog(@"cell frame %@", NSStringFromCGRect(self.frame)); // {{0, 0}, {375, 280}}
    NSLog(@"self.mainLabel frame %@", NSStringFromCGRect(self.mainLabel.frame)); // {{8, 8}, {304, 264}}

    [self.mainLabel updatePreferredMaxLayoutWidth];
}
如果框架宽度与 preferredMaxLayoutWidth

不同,

updatePreferredMaxLayoutWidth 是更新 preferredMaxLayoutWidth 的类别

-(void)updatePreferredMaxLayoutWidth {
    if (self.numberOfLines == 0) {
        if ( self.preferredMaxLayoutWidth != self.frame.size.width){
            self.preferredMaxLayoutWidth = self.frame.size.width;
            [self setNeedsUpdateConstraints];
        }
    }
}

现在当我 运行 我得到输出:

cell frame {{0, 0}, {375, 280}}

layoutSubviews 中的单元格大小与 table 视图的宽度相同,正确

self.mainLabel frame {{8, 8}, {304, 264}}

self.mainLabel layoutSubviews 中的 frame 为 304,即等于 xib 文件中的标签大小。所以标签的框架没有更新为新单元格的大小。为什么?

size {290.5, 117.5}

systemLayoutSizeFittingSize 的大小完全错误。我错过了什么?

呼叫

[self.contentView layoutIfNeeded]; 

在单元格的 layoutSubviews 中,解决了这个问题。

来源Table View Cells With Varying Row Heights

您可以使用此 class 解决 UILabel 和 AutoLayout 的所有问题

class UILabelPreferedWidth : UILabel {
    override var bounds: CGRect {
        didSet {
            if (bounds.size.width != oldValue.size.width) {
                self.setNeedsUpdateConstraints()
            }
        }
    }

    override func updateConstraints() {
        if(preferredMaxLayoutWidth != bounds.size.width) {
            preferredMaxLayoutWidth = bounds.size.width
        }
        super.updateConstraints()
    }
}

我使用 systemLayoutSizeFittingSize:withHorizontalFittingPriority:verticalFittingPriority: 解决了这个问题。在我的例子中,我想计算一个单元格的高度,所以我将水平优先级设置为 UILayoutPriorityRequired,将垂直优先级设置为 UILayoutPriorityFittingSizeLevel,最后我得到了正确的数字。