我有一个 UILabel
包含在一个具有固定大小的 UIView
中。 UILabel
具有动态文本。我如何确保 UILabel
永远不会比包含它的 UIView 大?
在使用 AutoLayout
之前,这很简单,只需使用 AdjustsFontSizeToFitWidth
即可实现,但我无法再使用 Autolayout 来实现这一点。
我应该从 UILabel
添加什么约束到 UIView
?现在它只是领先对齐。
最佳答案
像 UILabel
(contentHugging
和 contentCompressionResistance
)这样的 View 有特殊的方法,但它们已经启用。所以你只需要禁用标签的宽度增长到 super View 的宽度。
NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:self.label attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationLessThanOrEqual toItem:self.label.superview attribute:NSLayoutAttributeWidth multiplier:1.0f constant:0.0f];
[self.label addConstraint:widthConstraint];
关于iOS - 使用自动布局设置 UILabel 的最大宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34088683/