多行UIButton,每行都被截断

时间:2016-11-26 23:58:52

标签: ios uibutton uilabel truncate

我试图通过继承UIButton来创建一个多行按钮。为了避免绘制两个自定义UILabel(我还是Swift / Xcode的新手),我使用现有UILabel的属性字符串并使用换行符分割线条,像这样:

func prepareAttributedTitle(_ primaryTitle: String = "", _ secondaryTitle: String = "") {
    let title = NSMutableAttributedString()
    let first = NSAttributedString(string: primaryTitle, attributes: [
        NSForegroundColorAttributeName: tintColor,
        NSFontAttributeName: UIFont.systemFont(ofSize: UIFont.systemFontSize, weight: UIFontWeightSemibold)
    ])
    let newLine = NSAttributedString(string: "\n")
    let second = NSAttributedString(string: secondaryTitle, attributes: [
        NSForegroundColorAttributeName: tintColor.withAlphaComponent(0.75),
        NSFontAttributeName: UIFont.systemFont(ofSize: UIFont.smallSystemFontSize)
    ])

    title.append(first)
    title.append(newLine)
    title.append(second)

    setAttributedTitle(title, for: .normal)
}

结果是(抱歉,我没有足够的代表发布图片):

| This is the long first |
| line                   |
| Secondary line         |

但是,我想单独截断行,如下所示:

| This is the long fi... |
| Secondary line         |

有没有办法在不使用两个自定义UILabel的情况下执行此操作?

由于

1 个答案:

答案 0 :(得分:0)

单个UILabel不支持您需要的内容。您将不得不使用两个单行标签,每个标签都设有尾部截断。