【问题标题】:UILabel text not automatically resized using Auto LayoutUILabel 文本未使用自动布局自动调整大小
【发布时间】:2013-06-14 00:17:56
【问题描述】:

我正在尝试实现一个受约束的UITableViewCell 子类,并且除了UILabel 之外,一切都运行良好。我设置的约束肯定会被强制执行,但是当约束发生冲突时,标签内的文本不会调整为较小的字体大小。相反,UILabel 的高度被截断,字体保持不变,这意味着字母在顶部和底部被截断。

我必须调用一些方法才能使其正常工作吗?我认为 Auto Layout 足够聪明,可以自动调整字体大小,所以我有点不知道为什么会这样。

相关代码:

self.label = [[UILabel alloc] initWithFrame:CGRectZero];
self.label.textColor = [UIColor whiteColor];
self.label.translatesAutoresizingMaskIntoConstraints = NO;
self.label.textAlignment = NSTextAlignmentCenter;
self.label.numberOfLines = 1;
[self.contentView addSubview:self.label];

NSLayoutConstraint *otherViewToLabelHorizontalConstraint =  // Make sure that the label is always to the right of the other view.
                    [NSLayoutConstraint constraintWithItem:self.label 
                                                 attribute:NSLayoutAttributeLeft 
                                                 relatedBy:NSLayoutRelationGreaterThanOrEqual
                                                    toItem:self.otherView 
                                                 attribute:NSLayoutAttributeRight 
                                                multiplier:1.0
                                                  constant:0.0];

NSLayoutConstraint *aTextFieldToLabelVerticalConstraint = 
                    [NSLayoutConstraint constraintWithItem:self.label 
                                                 attribute:NSLayoutAttributeTop 
                                                 relatedBy:NSLayoutRelationGreaterThanOrEqual
                                                    toItem:self.aTextField 
                                                 attribute:NSLayoutAttributeBottom 
                                                multiplier:1.0
                                                  constant:0.0];

基本上,这些约束旨在强制一个单元格,其中otherView 在左侧,aTextField 在同一 y 级别上 otherView 的右侧,并且标签低于 aTextFieldotherView的底部右侧。

像往常一样,感谢您对此提供的任何帮助。

【问题讨论】:

  • 添加这些约束时,您是否在控制台中收到异常或其他日志消息?
  • 不,不过,我最终摆脱了使用此约束的部分。

标签: ios uilabel autolayout nslayoutconstraint


【解决方案1】:

你需要

myLabel.adjustsFontSizeToFitWidth = YES;
myLabel.minimumScaleFactor = .5f;

然后标签字体大小会自动调整。

【讨论】:

  • 感谢您的回复,但是在执行此代码后,它仍然在顶部和底部被切断。我的字体大小为 18.0f。我已经测试过字体,它需要适应的最小值是 13.0f,所以我不明白为什么在达到 0.5f 乘数之前它不会自动缩小它。
【解决方案2】:

我通过为链接标签的视图设置所需的宽度约束来解决这个问题。

   NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0f constant:width];

并确保此约束的优先级设置为 UILayoutPriorityRequired。

[constraint setPriority:UILayoutPriorityRequired];

在将标签固定到超级视图而不是文本字段时,文本正在调整大小,因此我推断标签最终宽度的布局计算中涉及的元素的固有大小一定存在问题。

【讨论】:

  • constant:size 中的size 是什么?
  • 这是您要约束的实际视图的宽度。我将其更改为宽度,因为它应该是。感谢您注意到错误
【解决方案3】:

除了adjustsFontSizeToFitWidthminimumScaleFactor的设置,还需要将标签的内容压缩设置为低优先级。查看此objc.io essay 的“抗压性和内容拥抱”部分。基本上,如果您的标签的抗压优先级低于尾随约束,则标签应该调整大小:

[myLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow
                                 forAxis:UILayoutConstraintAxisHorizontal];

【讨论】:

  • 如果标签被截断,您希望抗压强度高,而不是低。不是吗?
猜你喜欢
  • 2014-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-29
  • 2014-05-04
  • 2013-01-02
相关资源
最近更新 更多