ios - 将字体大小缩放到标签 iOS Swift 的宽度

标签 ios swift ios10

所以,我最近开始学习 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/

相关文章:

swift - UIButton 标题未显示在 UIStackView 内

ios - 如何使用 PHImageManager 获取原始图像

ios - Xcode 错误 : Code signing is required for product type 'Application' in SDK 'iOS 10.0'

ios - iPhone 上的复选框 : Left Border Cut Off

ios - Objective C将项目添加到nsmutablearray返回nil

ios - iOS 上 OpenGL ES 3.0 中 texture3D 的插值

ipad - 片段着色器未读取顶点着色器 'v_gradient' 的输出

ios - 如何设置单元测试来测试自定义 DJI 任务?

ios - segmentcontrol 没有在 ios swift 中选择

任何类型的 Swift 泛型