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;

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

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...