动态UILabel大小iOS 7问题

我正在尝试根据文本高度动态调整标签大小。 UILabel的高度可以从0到多行不等。 我想出了一个解决这个问题的解决方案,在iOS 8上工作正常但在iOS 7.1上失败了,我也试图支持。

Autolayout未在此项目中使用,所有约束都以编程方式完成。

代码如下:

//TableDelegate.m

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 85.0f; } 

//CustomTableViewCell.m

 -(UILabel *)commentTextLabel { if(!_commentTextLabel) { _commentTextLabel = [UILabel new]; _commentTextLabel.numberOfLines = 0; _commentTextLabel.translatesAutoresizingMaskIntoConstraints = NO; } return _commentTextLabel; } -(void)setupViews { [self.contentView addSubview:self.profilePictureView]; [self.contentView addSubview:self.userName]; [self.contentView addSubview:self.timePublishedLabel]; [self.contentView addSubview:self.commentTextLabel]; [self.contentView addSubview:self.seeMoreButton]; self.backgroundColor = [UIColor salooteInputTextBg]; self.contentView.backgroundColor = [UIColor salooteInputTextBg]; NSDictionary *views = @ { @"picture" : self.profilePictureView, @"userName" : self.userName, @"timePublished" : self.timePublishedLabel, @"text" : self.commentTextLabel, @"seeMore" : self.seeMoreButton }; [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[picture(38)]-5-[userName]-5-[timePublished]-5-|" options:0 metrics:nil views:views]]; [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[picture]-5-[text]-5-|" options:0 metrics:nil views:views]]; [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[seeMore]-5-|" options:0 metrics:nil views:views]]; [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[userName]-5-[text]-5-[seeMore]-5-|" options:0 metrics:nil views:views]]; [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[picture(38)]" options:0 metrics:nil views:views]]; } -(void)updateConstraints { [super updateConstraints]; } 

iOS 8结果(左)iOS 7.1结果(右)

在此处输入图像描述在此处输入图像描述 我没有在我的UILabel代码中设置任何高度约束,而是试图让约束调整我的垂直高度。 如果有人对如何在iOS 7.1上正常工作有一些意见,我会非常感激。

将约束移动到setupViews会产生以下结果:(iOS 7.1顶级iOS 8底部)

在此处输入图像描述

在此处输入图像描述

在我看来,你没有在commentTextLabel添加垂直约束? 你只有这个:

 //Comment text [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[picture]-5-[text]-0-|" options:0 metrics:nil views:views]]; 

尝试设置一个垂直约束 – 很可能你得到的约束错误不足,iOS 8猜测高度比iOS 7更好。另外,如果你要为视图添加约束,你不应该打电话getter中的sizeToFit

Autolayout未在此项目中使用,所有约束都以编程方式完成。

即使您只是以编程方式添加约束,您仍然在使用Autolayout。 🙂


回应编辑

您的垂直高度约束不足 – 您只指定了commentTextLabel的高度,但没有指定其y坐标。 请记住,Autolayout的主要目标是提供一组完整的约束,以便iOS可以计算视图的x,y,宽度和高度。

我认为你的约束总体上已被搞砸了。 :)尝试将这些规则添加到内容视图中(我只使用5表示任何填充):

 H:|-5-[picture(38)]-5-[username]-5-[timePublished]-5-| H:[picture]-5-[text]-5-| H:|-5-[seeMore]-5-| V:|-5-[username]-5-[text]-5-[seeMore]-5-| V:|-5-[picture(38)] 

此外,在setupViews中添加约束 – 您只需添加一次约束,并且只需在updateConstraints修改它们。 我认为每次调用layoutSubviews都会调用updateConstraints因此每次刷新单元格的布局时都会增加约束。


回应编辑

您的标签的自动换行样式也必须设置。 从commentTextLabel里面,添加

 _commentTextLabel.lineBreakMode = NSLineBreakByWordWrapping; 

如果您想要具有动态高度的UILabel,请始终将其设置为numberOfLines = 0

您还需要通过设置标签的alignment属性来右对齐seeMore标签(它占据单元格的整个宽度减去填充)。

并尝试提供更大的虚拟高度 – 可能是150或200而不是85,这样我们就可以看到所有的元素。

对于timePublished标签,我忘了指出以下垂直约束:

 V:|-5-[timePublished] 

我发现,轻松支持iOS7和iOS8的唯一方法是使用屏幕外原型自己进行每个单元的高度计算。 以下是关于这些问题的优秀文章。 我无法在iOS8中将自动布局高度计算与iOS7的手动高度估算混合在一个代码库中。

在UITableView中使用自动布局来获取动态单元格布局和可变行高

我使用此方法的唯一问题是当我使用大小类来更改单元格字体大小以便我可以在iPad等上使用更大的字体…此问题在此处讨论:

屏幕外UITableViewCells(用于大小计算)不尊重大小类?