我需要通过编程方式设置UIButton标题UILabel的字体大小。
当前回答
这会很有帮助
button.titleLabel.font = [UIFont fontWithName:@"YOUR FONTNAME" size:12.0f]
其他回答
objective - c:
[button.titleLabel setFont: [button.titleLabel.font fontWithSize: sizeYouWant]];
迅速:
button.titleLabel?.font = button.titleLabel?.font.fontWithSize(sizeYouWant)
只会改变字体大小。
快4.倍
button.titleLabel?.font = UIFont.systemFont(ofSize: 20)
斯威夫特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
希望能有所帮助。
支持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))
迅速:
shareButton.titleLabel?.font = UIFont.systemFontOfSize(size)
重要注意事项: 删除由animuson♦12月5日14日16:48 animuson,在我发布这个答案一个月后,我现在也有同样的问题。我在谷歌上搜索,发现这篇文章不容易复制粘贴到一个快速的项目中。当我滚动看到我删除的答案,并复制它。所以请不要删除真正有用的东西。
推荐文章
- Xcode构建失败“架构x86_64未定义的符号”
- 如何使用Xcode创建。ipa文件?
- 动态改变UILabel的字体大小
- registerForRemoteNotificationTypes: iOS 8.0及以上版本不支持
- 新的自动引用计数机制是如何工作的?
- 如何测试对象在Objective-C中的类?
- 在iPhone上确定用户是否启用了推送通知
- 是否有可能禁用浮动头在UITableView与UITableViewStylePlain?
- 从Cocoa应用程序执行一个终端命令
- 错误ITMS-9000:“冗余二进制文件上传。火车1.0版本已经有一个二进制版本上传。
- Swift -转换为绝对值
- Swift编译器错误:“框架模块内的非模块化头”
- 从父iOS访问容器视图控制器
- 自定义dealloc和ARC (Objective-C)
- 调整UITableView的大小以适应内容