Set UIButton title UILabel font size programmatically

前端 未结 18 3046
灰色年华
灰色年华 2020-12-07 07:16

I need to set the font size of the title UILabel of a UIButton programmatically.

18条回答
  •  离开以前
    2020-12-07 08:02

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

提交回复
热议问题