gpt4 book ai didi

iOS - 使用自动布局设置 UILabel 的最大宽度

转载 作者:可可西里 更新时间:2023-11-01 03:55:45 25 4
gpt4 key购买 nike

我有一个 UILabel 包含在一个具有固定大小的 UIView 中。 UILabel 具有动态文本。我如何确保 UILabel 永远不会比包含它的 UIView 大?

在使用 AutoLayout 之前,这很简单,只需使用 AdjustsFontSizeToFitWidth 即可实现,但我无法再使用 Autolayout 来实现这一点。

我应该从 UILabel 添加什么约束到 UIView?现在它只是领先对齐。

最佳答案

UILabel(contentHuggingcontentCompressionResistance)这样的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com