I need to set the font size of the title UILabel
of a UIButton
programmatically.
To support the Accessibility feature in UIButton
extension UILabel
{
func scaledFont(for font: UIFont) -> UIFont {
if #available(iOS 11.0, *) {
return UIFontMetrics.default.scaledFont(for: font)
} else {
return font.withSize(scaler * font.pointSize)
}
}
func customFontScaleFactor(font : UIFont) {
translatesAutoresizingMaskIntoConstraints = false
self.adjustsFontForContentSizeCategory = true
self.font = FontMetrics.scaledFont(for: font)
}
}
You can can button font now.
UIButton().titleLabel?.customFontScaleFactor(font: UIFont.systemFont(ofSize: 12))