简体   繁体   English

AutoLayout + heightForRowAtIndexPath / sizeWithFont无法正常工作

[英]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: 在我的tableViewDelegate中, heightForRowAtIndexPath:是这样计算的:

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? 我想念什么?

When I first faced this issue in iOS7 I've tried all possible ways to correctly estimate size of a wrapped or attributed text. 当我第一次在iOS7中遇到此问题时,我尝试了所有可能的方法来正确估计包裹或属性文本的大小。

And it appeared that the only accurate way is sizeThatFits: 似乎唯一准确的方法是sizeThatFits:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM