ios UIButton标题扩展限制

u2nhd7ah  于 2023-03-14  发布在  iOS
关注(0)|答案(1)|浏览(167)

我在使用UIButtonConfiguration控制UIButton的标题扩展限制时遇到了麻烦。下面是我的按钮的设置代码:

private lazy var button: UIButton = {
    let btn = UIButton()

    self.view.addSubview(btn)

    if #available(iOS 15.0, *) {
        var configuration = UIButton.Configuration.plain()
        configuration.contentInsets = NSDirectionalEdgeInsets(top: 1.0, leading: 12.0, bottom: 1.0, trailing: 12.0)
        configuration.imagePadding = 12.0
        btn.configuration = configuration
    } else {
        btn.imageEdgeInsets = UIEdgeInsets(top: 0, left: -12, bottom: 0, right: 0)
        btn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 12, bottom: 0, right: 0)
    }

    btn.translatesAutoresizingMaskIntoConstraints = false

    return btn
}()

出于好奇,我想测试一下当标题太长时它的行为:

self.button.setTitle("DoneDoneDoneDoneDoneDoneDoneDoneDoneDoneDoneDoneDoneDoneDoneDone", for: .normal)

NSLayoutConstraint.activate([
    self.button.heightAnchor.constraint(equalToConstant: 32.0),
    self.button.widthAnchor.constraint(equalToConstant: 96.0),
    self.button.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
    self.button.centerYAnchor.constraint(equalTo: self.view.centerYAnchor),
])

Here's the result
我如何调整它的内容扩展限制?我希望它像iOS 15.0之前的UIButton标题一样运行(用1行截断标签)
我试过将UIButtontitleLabel设置为1,将lineBreakMode设置为.byTruncatingTail,但是没有任何效果。

yqlxgs2m

yqlxgs2m1#

首先,值得注意的是,“deprecated”是一个警告-而不是一个错误
其次,同样值得注意的是,完整信息包括:

  • iOS 15.0中已弃用“imageEdgeInsets”:使用UIButtonConfiguration时忽略此属性

这意味着,当不使用UIButtonConfiguration时,不会忽略它。
第三,* 作为一般规则 *,你可能会更乐意使用单一按钮类型...因为它将在iOS版本之间保持功能和外观的一致性。
所以,就目前而言,我建议你坚持使用“老式”按钮。
尽管如此,你还是可以给予这个...
UIButton子类:

class MyForcedSingleLineButton: UIButton {
    
    public var lineBreakMode: NSLineBreakMode = .byTruncatingTail
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        if let tl = titleLabel {
            var r = tl.frame
            r.size.height = self.bounds.height
            r.origin.y = 0
            r.size.width = self.bounds.width - 24.0
            r.origin.x = 12.0
            tl.frame = r
            tl.numberOfLines = 1
            tl.lineBreakMode = lineBreakMode
        }
    }
    
}

许多“内置”视图/控件在幕后做了大量的工作,UIButton也不例外。UIKit会不断地“重置”标题标签属性,所以我们每次得到layoutSubviews()时都会进行调整。
下面是一个简单的控制器示例-我正在创建八个按钮:四个“新”样式各两个,一个为.byTruncatingTail,另一个为.byTruncatingMiddle。每个按钮的(完整)标题为 “这需要截断而不是换行到多行!"

class ButtonTestVC: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let stack = UIStackView()
        stack.axis = .vertical
        stack.spacing = 20
        
        let t: String = "This needs to truncate instead of wrapping onto multiple lines!"
        
        let styles: [UIButton.Configuration] = [
            .plain(), .gray(), .tinted(), .filled()
        ]
        
        var btn: MyForcedSingleLineButton!
        
        // let's add two buttons of each Configuration style
        //  one with .byTruncatingTail
        //  one with .byTruncatingMiddle
        styles.forEach { style in
            
            var cfg = style
            cfg.title = t
            
            btn = MyForcedSingleLineButton()
            btn.configuration = cfg
            
            btn.heightAnchor.constraint(equalToConstant: 32.0).isActive = true
            btn.widthAnchor.constraint(equalToConstant: 128.0).isActive = true
            
            btn.lineBreakMode = .byTruncatingTail
            
            stack.addArrangedSubview(btn)

            btn = MyForcedSingleLineButton()
            btn.configuration = cfg
            
            btn.heightAnchor.constraint(equalToConstant: 32.0).isActive = true
            btn.widthAnchor.constraint(equalToConstant: 128.0).isActive = true

            btn.lineBreakMode = .byTruncatingMiddle
            
            stack.addArrangedSubview(btn)
            
        }

        stack.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(stack)
        
        NSLayoutConstraint.activate([
            stack.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            stack.centerYAnchor.constraint(equalTo: view.centerYAnchor),
        ])
        
    }
    
}

它看起来是这样的:

这个定制的MyForcedSingleLineButton子类并不完整(希望是显而易见的)......您可能希望使用动态值来调整它,以反映定制的插图、图像的使用等。

相关问题