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


当前回答

迅速:

shareButton.titleLabel?.font = UIFont.systemFontOfSize(size)

重要注意事项: 删除由animuson♦12月5日14日16:48 animuson,在我发布这个答案一个月后,我现在也有同样的问题。我在谷歌上搜索,发现这篇文章不容易复制粘贴到一个快速的项目中。当我滚动看到我删除的答案,并复制它。所以请不要删除真正有用的东西。

其他回答

支持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:14.0];

这可能会有帮助:

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

应该帮助

斯威夫特5。

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