【问题标题】:Swift label not fitting text correctlySwift 标签无法正确拟合文本
【发布时间】:2018-12-06 13:57:10
【问题描述】:

我正在尝试创建一个标签,该标签将能够显示不同大小的不同电影标题。当我使用下面的代码时

lbl.numberOfLines = 3
lbl.adjustsFontSizeToFitWidth = true
lbl.minimumScaleFactor = 0.05

我遇到了较长的单词被分成多行的问题

查看以下问题:

第一个屏幕截图

当我使用此代码时:

lbl.lineBreakMode = .byWordWrapping
lbl.numberOfLines = 3
lbl.adjustsFontSizeToFitWidth = true
lbl.minimumScaleFactor = 0.05

我遇到了单词被遗漏的问题(标签应该显示文本“What's Love Got to Do With It”)

查看以下问题:

第二张屏幕截图

标签有一组简单的约束

lbl.topAnchor.constraint(equalTo: countdownLbl.bottomAnchor, constant: 20).isActive = true
lbl.bottomAnchor.constraint(equalTo: btnStackView.topAnchor, constant: 20).isActive = true
lbl.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 20).isActive = true
lbl.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 20).isActive = true

任何帮助将不胜感激。

【问题讨论】:

  • 只有当 numberOfLines 设置为 1 时,设置 minimumScaleFactor 才能按预期工作。 This 可能就是您要找的。​​span>

标签: swift text resize uilabel


【解决方案1】:

在 Interface Builder 的小框中键入时使用 option-return 来插入换行符 (\n)。在 Interface Builder 的 Label 属性中,设置 # Lines = 0。

选择标签,然后将 Lines 属性更改为 0。

如上图所示,然后在字符串中使用 \n 换行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-23
    • 1970-01-01
    相关资源
    最近更新 更多