文字的UILabel使用自动布局不自动调整大小

文字的UILabel使用自动布局不自动调整大小

本文介绍了文字的UILabel使用自动布局不自动调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个约束的UITableViewCell 子,一切都可以正常使用,除了的UILabel 。我已经设置了约束肯定是被强制执行,但是当限制冲突的标签内的文本不会调整到一个较小的字体大小。取而代之的是,的UILabel的高度被截断和字体大小保持不变,这意味着字母得到在顶部和底部切断

I'm trying to implement a constrained UITableViewCell subclass and everything is working perfectly, except for the UILabel. The constraints that I've set up are definitely being enforced, but the text inside of the label does not resize to a smaller font size when the constraints clash. Instead, the height of the UILabel gets truncated and the font remains the same size, meaning the letters get cut off at the top and bottom.

有没有一些方法,我为了得到这个工作打电话?我想自动布局将是足够聪明的自动调整字体大小,所以我有点失去了,为什么发生这种情况。

Is there some method I have to call in order to get this to work? I would think Auto Layout would be smart enough to automatically resize the font size, so I'm kind of lost as to why this is happening.

相关code:

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 是在右 otherView 在相同的Y级,标签低于 aTextField 和底部的权利 otherView

Basically, these constraints are meant to enforce a cell where otherView is on the left, aTextField is on the right of otherView at the same y-level, and the label is below aTextField and to the right of the bottom of otherView.

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

As usual, thanks for any help with this.

推荐答案

您需要

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

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

Then the label font size will be auto adjusted.

这篇关于文字的UILabel使用自动布局不自动调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 01:28