【问题标题】:UIButton title label font weight is not changing to BOLD inside custom UITableViewHeaderFooterView Subclass自定义 UITableViewHeaderFooterView 子类中的 UIButton 标题标签字体粗体不会更改为 BOLD
【发布时间】:2017-07-31 10:03:14
【问题描述】:

在我的程序中,我想在自定义 UITableViewHeaderFooterView 子类中将粗体系统字体应用于 UIButton 的 titleLabel。但每次字体都显示有规律的重量。

注意提示:对于 UIBUTTON,我创建了 UIBUTTON 的子类(命名为 BUTTON,用于自动调整字体大小以适应 UIBUTTON 的框架高度)。当我使用 UIBUTTON 代替按钮时,粗体字体会正确应用。就喜欢

private let createVmButton: UIButton = {
        let button = UIButton(type: .system)
        button.backgroundColor = UIColor(hex: 0x0083C5, alpha: 1)
        button.setTitle("Create VM", for: .normal)
        button.setTitleColor(UIColor.white, for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 25, weight: UIFontWeightBold)
        button.layer.cornerRadius = 5
        button.translatesAutoresizingMaskIntoConstraints = false
        return button
    }()

完整程序: UIButton 子类:

class Button: UIButton {

    var fontSize: CGFloat = 0
    var frameHeight: CGFloat = 0

    override func layoutSubviews() {
        super.layoutSubviews()
        if let titleLabel = titleLabel {
            titleLabel.font = titleLabel.font.withSize(frame.size.height * (fontSize / frameHeight))
        }
    }
}

UITableViewHeaderFooterView 子类:

class VmListFooter: UITableViewHeaderFooterView {

    private let createVmButton: Button = {
        let button = Button(type: .system)
        button.backgroundColor = UIColor(hex: 0x0083C5, alpha: 1)
        button.setTitle("Create VM", for: .normal)
        button.setTitleColor(UIColor.white, for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 25, weight: UIFontWeightBold)
        button.layer.cornerRadius = 5
        button.fontSize = 25
        button.frameHeight = 60
        button.translatesAutoresizingMaskIntoConstraints = false
        return button
    }()

    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)

        contentView.backgroundColor = UIColor(hex: 0xF1F1F1, alpha: 1)
        setUpViews()
        createVmButton.addTarget(self, action: #selector(onCreateVmPressed(sender: )), for: .touchUpInside)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func onCreateVmPressed(sender: Button) {
        print("VM Create Button Pressed")
    }

    private func setUpViews() {
        contentView.addSubview(createVmButton)

        // x, y, width, height => createVmButton
        contentView.addConstraint(NSLayoutConstraint(
            item: createVmButton,
            attribute: .centerX,
            relatedBy: .equal,
            toItem: contentView,
            attribute: .centerX,
            multiplier: 1,
            constant: 0
        ))
        contentView.addConstraint(NSLayoutConstraint(
            item: createVmButton,
            attribute: .centerY,
            relatedBy: .equal,
            toItem: contentView,
            attribute: .centerY,
            multiplier: 1,
            constant: 0
        ))
        contentView.addConstraint(NSLayoutConstraint(
            item: createVmButton,
            attribute: .width,
            relatedBy: .equal,
            toItem: contentView,
            attribute: .width,
            multiplier: (374.0 / 414.0),
            constant: 0
        ))
        createVmButton.addConstraint(NSLayoutConstraint(
            item: createVmButton,
            attribute: .height,
            relatedBy: .equal,
            toItem: createVmButton,
            attribute: .width,
            multiplier: (60.0 / 374.0),
            constant: 0
        ))  // aspect ratio

        contentView.layoutIfNeeded()
    }
}

【问题讨论】:

    标签: ios swift swift3


    【解决方案1】:

    将代码更改为:

    class Button: UIButton {
    
        var fontSize: CGFloat = 0
        var frameHeight: CGFloat = 0
    
        override func layoutSubviews() {
            super.layoutSubviews()
            titleLabel?.font = UIFont.boldSystemFont(ofSize: frame.size.height * (fontSize / frameHeight))
        }
    }
    
    
    private let createVmButton: Button = {
            let button = Button(type: .system)
            button.backgroundColor = UIColor(hex: 0x0083C5, alpha: 1)
            button.setTitle("Create VM", for: .normal)
            button.setTitleColor(UIColor.white, for: .normal)
            button.layer.cornerRadius = 5
            button.fontSize = 30
            button.frameHeight = 60
            button.translatesAutoresizingMaskIntoConstraints = false
            return button
        }()
    

    【讨论】:

    • 感谢您的回答,但在我的程序中,我还有其他地方也使用过这个“按钮”类。如果我在这里强行应用 BOLD fontweight,那也会反映到其他代码中
    • @Sanket 所以我认为你的代码也可以工作。我已经在我的模拟器上尝试了你的代码,它可以工作!
    • 是的,除了这个 UITableViewHeaderFooterView 子类之外,我的代码在任何地方都可以使用。我不知道是什么问题。我已经在我的 IPHONE DEVICE 上测试了结果。我遇到了这个问题。
    猜你喜欢
    • 2023-03-31
    • 1970-01-01
    • 2011-05-13
    • 2017-09-05
    • 2021-12-25
    • 2020-12-22
    • 2013-08-06
    • 1970-01-01
    • 2021-03-19
    相关资源
    最近更新 更多