我正在尝试实现一个受限的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
在左侧,aTextField
在otherView
的右侧处于同一y级别,并且标签位于aTextField
下方并在otherView
底部的右侧。
像往常一样,感谢您对此的任何帮助。