所以,我最近开始学习 Swift,想知道如何根据屏幕尺寸缩放标签内的文本。我见过几个类似的问题,但它们看起来相当复杂。我正在尝试这样做,但这似乎不起作用。
UIlabel1.adjustsFontSizeToFitWidth = true
我已将标签连接到代码并设置了一些约束条件。 等宽约束 等高约束 中心 X 对齐约束 垂直空间约束
约束链接到 View Controller ,以便实际标签大小 随屏幕尺寸缩放。有什么想法吗?
最佳答案
来自 adjustsFontSizeToFitWidth
的文档:
Normally, the label text is drawn with the font you specify in the font property. If this property is set to true, and the text in the text property exceeds the label’s bounding rectangle, the label reduces the font size until the string fits or the minimum font scale is reached. The default value for this property is false. If you change it to true, be sure that you also set an appropriate minimum font scale by modifying the minimumScaleFactor property.
这表示在使用 adjustsFontSizeToWidth
时必须设置 minimumScaleFactor
才能缩放字体。
但请注意,这种缩放只是一个方向。如果需要显示您的文本,您的字体大小将变得更小,但不会大于您设置的字体大小。
鉴于此,请尝试将字体大小设置为适合您的应用运行的最大屏幕的值。在较小的屏幕上,它会缩小以适应您为 minimumScaleFactor
指定的限制。
关于ios - 将字体大小缩放到标签 iOS Swift 的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43905585/