我需要通过编程方式设置UIButton标题UILabel的字体大小。


当前回答

【按钮setFont:……已被弃用。

使用[按钮。titleLabel setFont:…而是,例如:

[myButton.titleLabel setFont:[UIFont systemFontOfSize:10]];

其他回答

检查自定义字体名称是否添加了“目标成员资格”复选框。这应该会有所帮助。

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

你可以按钮字体现在。

UIButton().titleLabel?.customFontScaleFactor(font: UIFont.systemFont(ofSize: 12))
button.titleLabel.font = [UIFont systemFontOfSize:size];

应该帮助

快4.倍

button.titleLabel?.font = UIFont.systemFont(ofSize: 20)

希望对你有所帮助

[_button.titleLabel setFont:[UIFont systemFontOfSize:15]];  

祝你好运