使用自动布局时,UILabel文本不会自动调整大小

12

我正在尝试实现一个受限的UITableViewCell子类,除了UILabel之外,一切都正常工作。我已经设置好的约束肯定是被强制执行的,但是标签内的文本在约束冲突时不会缩小到更小的字体大小。相反,UILabel的高度被截断,字体大小保持不变,这意味着字母在顶部和底部被切断。

我需要调用一些方法来使其工作吗?我认为自动布局应该足够聪明,能够自动调整字体大小,所以我有点迷茫,不知道为什么会出现这种情况。

相关代码:

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在左侧,aTextFieldotherView的右侧处于同一y级别,并且标签位于aTextField下方并在otherView底部的右侧。

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


当添加这些约束时,您的控制台是否收到异常或其他日志消息? Translated text: - larsacus
没有,我最终放弃了使用这个约束的部分。 - J Kagney
3个回答

7

您需要:

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

那么标签字体大小将自动调整。

2
谢谢您的回复,但是在实施这段代码后,它仍然在顶部和底部被截断。我将字体大小设置为18.0f。我已经测试了字体,最小适合的大小是13.0f,所以我不明白为什么它不会自动缩小字体直到达到0.5f的乘数。 - J Kagney

3
我通过为我将标签链接到的视图设置必需的宽度约束来解决了这个问题。
   NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0f constant:width];

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

[constraint setPriority:UILayoutPriorityRequired];

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


1
在“constant:size”中,“size”是什么? - jowie
你想要限制的是实际视图的宽度。我已将其更改为宽度,因为这样更合适。感谢您注意到了错误。 - Alejandro Benito-Santos

2
随着adjustsFontSizeToFitWidthminimumScaleFactor的设置,您还需要将标签的内容压缩设置为低优先级。请查看此objc.io essay中的“Compression Resistance and Content Hugging”部分。基本上,如果您的标签的压缩抵抗优先级低于尾部约束,则应调整标签大小:
[myLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow
                                 forAxis:UILayoutConstraintAxisHorizontal];

1
如果标签被截断,您希望压缩阻力高,而不是低。对吗? - SmileBot

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接