ios - 更改 UIBarButtonItem 的字体

标签 ios cocoa-touch uibarbuttonitem

UIToolbar with UIBarButtonItem

我的 UIToolbar 中有一个 UIBarButtonItem,标题为 Done。现在我想将字体从默认更改为 "Trebuchet MS" 加粗。我该怎么做?

最佳答案

准确的说,可以按照下面的方式来完成

[buttonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
    [UIFont fontWithName:@"Helvetica-Bold" size:26.0], NSFontAttributeName,
    [UIColor greenColor], NSForegroundColorAttributeName,
    nil] 
                          forState:UIControlStateNormal];

或者使用对象字面量语法:

[buttonItem setTitleTextAttributes:@{
     NSFontAttributeName: [UIFont fontWithName:@"Helvetica-Bold" size:26.0],
     NSForegroundColorAttributeName: [UIColor greenColor]
} forState:UIControlStateNormal];

为方便起见,这里是 Swift 实现:

buttonItem.setTitleTextAttributes([
        NSAttributedStringKey.font: UIFont(name: "Helvetica-Bold", size: 26.0)!,
        NSAttributedStringKey.foregroundColor: UIColor.green],
    for: .normal)

关于ios - 更改 UIBarButtonItem 的字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8849913/

相关文章:

iphone - 如何修改普通 UITableView 中 'empty' 单元格的外观?

ios - UIBarButtonItem:如何更改文本阴影偏移量?

ios - CustomView UIBarButtonItem 适用于 iPhone 6 以外的所有手机

ios - 一张带有 alpha 滞后 spritekit 的图像

ios - Swift 3 中的 UIButton 填充

ios - Swift 4 Codable , CodingKey 仅用于解码

ios - 同步 TabBar/NavigationBar 上的背景颜色

IOS,带有观察者的自定义表格 View 单元格,如何分离观察者

ios - IB - 将 UIView 添加到 UITableViewController?

ios - 我可以只使 tableview 的特定部分可点击吗?