UIBUTTON标签标签字体重量不会更改为BOLD内部自定义Viewheaderfooterview子类



在我的程序中,我想在自定义Uaithitviewheaderfooterview子类中应用粗体系统字体。但是每次字体都带有定期重量的字体。

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

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))
        }
    }
}

uitaiteViewHeaderFooterview子类:

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()
    }
}

将您的代码更改为:

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
    }()

最新更新