在我的程序中,我想在自定义UITableViewHeaderFooterView子类中将粗体系统字体应用于UIButton的titleLabel。但每次都有正常重量的字体显示。
注意提示:对于UIBUTTON,我创建了UIBUTTON的子组(被命名为按钮,用于自动调整FONT SIZE以适应UIBUTTON的框架高度)。当我在按钮的位置使用UIBUTTON时,BOLD FONT正确应用。只是喜欢
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 SUBCLASS:
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 SUBCLASS:
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()
}
}
答案 0 :(得分:0)
将您的代码更改为:
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
}()