UIButton 配置换行模式不起作用

问题描述 投票:0回答:2

我知道有一些关于此的 SO 帖子,但没有一个有效......

我只是想使用

UIButton.Configuration
方法获取我的自定义 UIButton 子类,以 强制我的标题标签保持 1 行,而不更改按钮框架。

我不断得到如下所示的正确按钮...

我能做什么??这是我通过按钮设置的方式..

init(withTitle title: String, ... <more custom params> ...) {

    // ...

    super.init(frame: .zero)

    var config = UIButton.Configuration.filled()
    config.title = title

    titleLabel?.lineBreakMode = .byTruncatingTail
    titleLabel?.numberOfLines = 1
                
    configurationUpdateHandler = { button in

        // ... here I handle colorizing elements for different button states /

    }

}

我知道我可以通过使用标准

let button = UIButton(type: .custom)
完成文本剪辑,并设置
titleLabel
行属性。这不是解决方案 - 否则无法使用配置提供的定制。

swift uibutton uilabel line-breaks
2个回答
1
投票

我不以此为荣

button.configurationUpdateHandler = { button in
    if let titleLabel = button.titleLabel {
        DispatchQueue.main.async {
            titleLabel.numberOfLines = 1
            titleLabel.lineBreakMode = .byTruncatingTail
        }
    }
}


0
投票

从 Xcode 14.3 开始,您可以通过

UIButton.Configuration
对象指定换行模式为:

var config = UIButton.Configuration.filled()
config.titleLineBreakMode = .byTruncatingTail
myButton.config = config
© www.soinside.com 2019 - 2024. All rights reserved.