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


当前回答

这可能会有帮助:

[objBtn.titleLabel setFont:[UIFont fontWithName:@“fontname” size:fontsize]];

其他回答

您还可以自定义按钮字体粗体,斜体。 本例采用系统粗体字体大小。

[LoginButton.titleLabel setFont:[UIFont boldSystemFontOfSize:15.0f*Ratio]];

快4.倍

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

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

斯威夫特5。

uiButton.titleLabel?.font = UIFont.systemFont(ofSize: 32)

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