无法让UILabel的文字留在多行

时间:2015-03-07 12:16:00

标签: ios swift uikit

我有一个UILabel应该在多行上显示文本,以防它太长而无法保持在一行上。这是我在界面构建器中设置其参数的方法:

enter image description here

但即使这样做,文本仍会被截断:

enter image description here

这是我在运行时设置文本的方式:

    let text = "left button pressed 5 seconds ago, you may want to press another button now"
    let attributedText = NSMutableAttributedString(string: text)
    attributedText.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFontOfSize(statusLabel.font.pointSize), range: (text as NSString).rangeOfString("left"))

    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.lineBreakMode = .ByWordWrapping
    attributedText.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, attributedText.length))
    statusLabel.attributedText = attributedText

就像你看到我甚至试图添加一个段落样式属性来强制文本保持多行,但它不起作用。

1 个答案:

答案 0 :(得分:1)

检查您是否设置了自动布局约束,以便定义顶部,前导和尾随空格,但不要连接垂直高度,标签将根据内容自行调整。

编辑:

enter image description here enter image description here