ios – UILabel文本不会使用自动布局自动调整大小

前端之家收集整理的这篇文章主要介绍了ios – UILabel文本不会使用自动布局自动调整大小前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图实现一个受限制的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];

基本上,这些约束意在强制执行其他view在左侧的单元格,aTextField位于同一y级别的otherView右侧,标签位于aTextField下方,在右侧的otherView底部.

像往常一样,感谢任何帮助.

解决方法

你需要
myLabel.adjustsFontSizeToFitWidth = YES;
myLabel.minimumScaleFactor = .5f;

然后标签字体大小将被自动调整.

原文链接:https://www.f2er.com/iOS/335718.html

猜你在找的iOS相关文章