动态UILabel大小iOS 7问题 [英] Dynamic UILabel size iOS 7 issue

查看:40
本文介绍了动态UILabel大小iOS 7问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据文本高度动态调整标签的大小.高度可以在0到UILabel中的许多行之间变化.我已经针对此问题提出了一个解决方案,该解决方案在iOS 8上运行良好,但在我也尝试支持的iOS 7.1上失败.

该项目未使用自动布局,并且所有约束均以编程方式完成.

代码如下:

//TableDelegate.m

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

//CustomTableViewCell.m

 -(UILabel *)commentTextLabel{如果(!_commentTextLabel){_commentTextLabel = [UILabel新];_commentTextLabel.numberOfLines = 0;_commentTextLabel.translatesAutoresizingMaskIntoConstraints = NO;}返回_commentTextLabel;}-(无效)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 *意见= @{@"picture":self.profilePictureView,@"userName":self.userName,@"timePublished":self.timePublishedLabel,@"text":self.commentTextLabel,@"seeMore":self.seeMoreButton};[self.contentView addConstraints:[NSLayoutConstraintconstraintsWithVisualFormat:@"H:| -5- [picture(38)]-5- [userName] -5- [timePublished] -5- |"选项:0指标:无视图:视图]];[self.contentView addConstraints:[NSLayoutConstraint ConstraintsWithVisualFormat:@"H:[picture] -5- [text] -5- |"选项:0指标:无视图:视图]];[self.contentView addConstraints:[NSLayoutConstraintconstraintsWithVisualFormat:@"H:| -5- [seeMore] -5- |"选项:0指标:无视图:视图]];[self.contentView addConstraints:[NSLayoutConstraint ConstraintsWithVisualFormat:@"V:| -5- [userName] -5- [text] -5- [seeMore] -5- |"选项:0指标:无视图:视图]];[self.contentView addConstraints:[NSLayoutConstraintconstraintsWithVisualFormat:@"V:| -5- [picture(38)]" options:0metrics:nil views:views]];}-(无效)updateConstraints{[super updateConstraints];} 

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

我没有在我的UILabel代码中设置任何高度限制,而是试图让这些限制为我调整垂直高度.如果有人对如何使其在iOS 7.1上正常工作有任何意见,我将不胜感激.

将约束移到setupViews中会产生以下结果:(iOS 7.1最高,iOS 8最低)

我发现,轻松支持iOS7和iOS8的唯一方法是使用屏幕外的原型自己计算每个单元的高度.以下是有关该问题的出色文章.我找不到在单一代码库中将iOS8的自动布局高度计算与iOS7的手动高度估算值相混合的方法.

使用UITableView中的自动布局可实现动态单元格布局和可变的行高

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

屏幕外的UITableViewCells(用于大小计算)不尊重尺码等级?

I'm trying resize a label dynamically according to text height. The height can vary from 0 to many lines in the UILabel. I've come up with a solution for this problem that works fine on iOS 8 but fails on iOS 7.1 which I'm trying to support as well.

Autolayout is not being used in this project and all constraints are done programatically.

The code is as follows:

//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 result (left) iOS 7.1 result (right)

I'm not setting any height constraint in my code for the UILabel but rather trying to let the constraints adjust the vertical height for me. If anyone has some input on how to make this work properly on iOS 7.1 I would really appreciate it.

Moving constraints into setupViews produces this: (iOS 7.1 top iOS 8 bottom)

解决方案

I have found that the only way to support both iOS7 and iOS8 easily, is to do the height calculations for each cell yourself using off screen prototypes. The following is an excellent article on the issues. I could find no way to mix auto layout height calculation from iOS8 with manual height estimates for iOS7 in a single code base.

Using Auto Layout in UITableView for dynamic cell layouts & variable row heights

The only issue I had with this method was when I used size classes to change the cell font sizes so I could have larger font on iPad etc... This issue is discussed here:

Offscreen UITableViewCells (for size calculations) not respecting size class?

这篇关于动态UILabel大小iOS 7问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆