我有以下代码…

UILabel *buttonLabel = [[UILabel alloc] initWithFrame:targetButton.bounds];
buttonLabel.text = @"Long text string";
[targetButton addSubview:buttonLabel];
[targetButton bringSubviewToFront:buttonLabel];

...这个想法是,我可以有多行文本的按钮,但文本总是被UIButton的backgroundImage掩盖。显示按钮子视图的日志调用显示UILabel已添加,但文本本身无法看到。这是UIButton的bug还是我做错了什么?


当前回答

基于@Amir Khorsandi的回答-但如果你有一个带有图像的按钮:

extension UIButton {

    func enableMultiline() {
        setContentHuggingPriority(.defaultLow, for: .vertical) // high hugging priority could squeeze the button to 1 line
        titleLabel?.numberOfLines = 0
        titleLabel?.setContentCompressionResistancePriority(.required, for: .vertical)
        titleLabel?.topAnchor.constraint(greaterThanOrEqualTo: topAnchor).isActive = true
        titleLabel?.bottomAnchor.constraint(lessThanOrEqualTo: bottomAnchor).isActive = true
        imageView?.setContentCompressionResistancePriority(.required, for: .horizontal) // otherwise image could be squeezed
    }

}

其他回答

设置lineBreakMode为nslinebreakbywordwrapped(在IB或代码中)使按钮标签多行,但不影响按钮的帧。

如果按钮有动态标题,有一个技巧:把隐藏的UILabel与相同的字体,并把它的高度与按钮的高度与布局;当设置文本到按钮和标签和自动布局将使所有的工作。

Note

单行按钮的固有尺寸高度大于标签的固有尺寸高度,因此为了防止标签的高度缩小,标签的垂直内容拥抱优先级必须大于按钮的垂直内容压缩阻力。

要修复按钮title标签的间距,在setTitle之前设置titleEdgeInsets和其他属性:

    let button = UIButton()
    button.titleLabel?.lineBreakMode = .byWordWrapping
    button.titleLabel?.numberOfLines = 0
    button.titleEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 20, right: 20)
    button.setTitle("Dummy button with long long long long long long long long title", for: .normal)

附注:我测试设置titleLabel?textAlignment是不必要的,标题对齐在.natural。

斯威夫特3

button.titleLabel?.lineBreakMode = .byWordWrapping
button.titleLabel?.textAlignment = .center  
button.setTitle("Button\nTitle",for: .normal)

现在,如果你真的需要在接口构建器中逐个访问这类东西,你可以用一个简单的扩展,像这样:

extension UIButton {
    @IBInspectable var numberOfLines: Int {
        get { return titleLabel?.numberOfLines ?? 1 }
        set { titleLabel?.numberOfLines = newValue }
    }
}

然后你可以简单地将numberolines设置为任何UIButton或UIButton子类的属性,就像它是一个标签一样。同样的道理也适用于其他一大堆通常无法访问的值,比如视图层的角半径,或者它所投射的阴影的属性。

重申一下Roger Nolan的建议,但使用显式代码,这是一般的解决方案:

button.titleLabel?.numberOfLines = 0