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


当前回答

这应该能让你振作起来

[btn_submit.titleLabel setFont:[UIFont systemFontOfSize:14.0f]];

其他回答

斯威夫特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))

这应该能让你振作起来

[btn_submit.titleLabel setFont:[UIFont systemFontOfSize:14.0f]];

这会很有帮助

button.titleLabel.font = [UIFont fontWithName:@"YOUR FONTNAME" size:12.0f]

斯威夫特4

button.titleLabel?.font = UIFont(name: "Font_Name_Here", size: Font_Size_Here)

objective - c

[button.titleLabel setFont:[UIFont fontWithName:@“Font_Name_Here” size: Font_Size_Here]];

例子:

Font_Name = "Helvetica"

Font_Size = 16.0

希望能有所帮助。