gpt4 book ai didi

ios - Swift 3 - 调整字体大小以适合宽度,多行

转载 作者:IT王子 更新时间:2023-10-29 05:52:22 30 4
gpt4 key购买 nike

我有一个 UILabel,它被设置为 42.0 pt 字体,标签的宽度是使用基于标签本身以外因素的自动约束设置的(也就是标签左右的东西决定了标签的宽度).

我想自动调整字体大小以适应标签的宽度,但也可以分两行。类似这样:

enter image description here

我知道您可以调整字体大小以适应标签的宽度,但前提是行数设置为 1。

我将如何完成这个?

最佳答案

这会起作用..

  • 为您的标签设置最小比例因子。如此图所示。
  • adjustsFontSizeToFitWidth设置为true
  • 设置行数 = 2//如果您想要更多行数,则设置为零 (0)
  • 将 2 行的换行模式设置为 '.byTruncatingTail'

swift 5
将动态文本信息的行数设置为零,这对于可变文本很有用。

var label = UILabel()
let stringValue = "A label\nwith\nmultiline text."
label.text = stringValue
label.numberOfLines = 2 // 0
label.lineBreakMode = .byTruncatingTail // or .byWrappingWord
label.minimumScaleFactor = 0.5 // It is not required but nice to have a minimum scale factor to fit text into label frame
label.adjustsFontSizeToFitWidth = true //needed in Swift 5

enter image description here

另外,不要为标签设置超过 2 行的高度限制。

关于ios - Swift 3 - 调整字体大小以适合宽度,多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42591699/

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