gpt4 book ai didi

ios - UILabel 文本不会使用自动布局自动调整大小

转载 作者:技术小花猫 更新时间:2023-10-29 10:36:39 27 4
gpt4 key购买 nike

我正在尝试实现一个受约束的 UITableViewCell 子类,除了 UILabel 之外,一切都运行良好。我设置的约束肯定会被强制执行,但是当约束发生冲突时,标签内的文本不会调整为较小的字体大小。相反,UILabel 的高度被截断,字体保持相同大小,这意味着字母在顶部和底部被截断。

为了让它工作,我必须调用一些方法吗?我认为 Auto Layout 会足够智能,可以自动调整字体大小,所以我不太明白为什么会这样。

相关代码:

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 底部的右侧。

像往常一样,感谢您对此的任何帮助。

最佳答案

你需要

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

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

关于ios - UILabel 文本不会使用自动布局自动调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17157778/

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