gpt4 book ai didi

ios - 在 UILabel 中更改字体时 UILabel 大小计算不正确

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

我正在将 UILabel 添加到具有以下布局要求的 View 中:

  • 标签应该居中
  • 标签的最大字体大小应为 100 磅,但应按比例缩小以适合。它不应被截断。
  • 标签的高度不应超过 450 点。
  • 另一个 View 将位于标签正下方。

我的标签的属性布局约束似乎充分描述了这一点:

let label = UILabel()
label.backgroundColor = UIColor.yellowColor()
label.font = UIFont.systemFontOfSize(100)
label.numberOfLines = 0
label.textAlignment = .Center
label.adjustsFontSizeToFitWidth = true
label.text = "What's Brewing in PET/CT: CT and MR Emphasis"
label.setTranslatesAutoresizingMaskIntoConstraints(false)
view.addSubview(label)

view.addConstraint(NSLayoutConstraint(item: label, attribute: .CenterX, relatedBy: .Equal, toItem: view, attribute: .CenterX, multiplier: 1, constant: 0))
view.addConstraint(NSLayoutConstraint(item: label, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .CenterY, multiplier: 1, constant: 0))
view.addConstraint(NSLayoutConstraint(item: label, attribute: .Left, relatedBy: .Equal, toItem: view, attribute: .Left, multiplier: 1, constant: 60))
view.addConstraint(NSLayoutConstraint(item: label, attribute: .Right, relatedBy: .Equal, toItem: view, attribute: .Right, multiplier: 1, constant: -60))
view.addConstraint(NSLayoutConstraint(item: label, attribute: .Height, relatedBy: .LessThanOrEqual, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 450))

但是,我发现对于如下所示的较长内容,标签显示时会出现一些意外的边距:

enter image description here

这是 Not Acceptable ,因为任何直接放置在标签上方或下方的 View 都会有间隙。

根据下面的讨论,我的工作理论是布局引擎根据字体大小 100 计算大小,而不考虑内容较长时的后续缩放。

如何满足上面列出的布局要求(最好不使用任何已弃用的方法)?

这是一个 test project一起玩。

最佳答案

我查看了您的项目,我可能找到了解决您问题的方法。

我在标签设置中添加了以下内容:

label.minimumScaleFactor = 0.5

这是我修改后的 View : enter image description here

标签顶部不再有间距。

希望这可以帮助您解决问题!让我知道进展如何。

关于ios - 在 UILabel 中更改字体时 UILabel 大小计算不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30061965/

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