If you are trying to do this with the new UIButton.configuration
in iOS 15, changing the titlelabel?.font
won't work because of the configuration
field will override it with a system style since you now need to set configuration?.title = "some title"
.
You need to create an AttributedString
and set configuration?.attributedTitle = yourAttributedString
My whole app is programmatic so here is an example of a custom button "red pill" style button I created that dynamically sets the button title with a convenience init.
class APButton: UIButton {
override init(frame: CGRect) {
super.init(frame: frame)
configure()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
convenience init(title: String) {
self.init(frame: .zero)
setDynamicTitle(title: title)
}
private func configure() {
configuration = .filled()
configuration?.cornerStyle = .capsule
configuration?.baseBackgroundColor = .red
configuration?.baseForegroundColor = .white
translatesAutoresizingMaskIntoConstraints = false
}
private func setDynamicTitle(title: String) {
let font = UIFont.systemFont(ofSize: 20)
let container = AttributeContainer([NSAttributedString.Key.font: font])
let attribString = AttributedString(title, attributes: container)
configuration?.attributedTitle = attribString
}
}