2012-01-13 59 views
110

UIToolbar with UIBarButtonItem更改的UIBarButtonItem

的字体我在我UIToolbar题为完成一个UIBarButtonItem。现在我想将字体从默认更改为“Trebuchet MS”加粗。我怎样才能做到这一点?

回答

107

因为从的UIBarButtonItem继承UIBarItem,你可以尝试

- (void)setTitleTextAttributes:(NSDictionary *)attributes 
        forState:(UIControlState)state 

,但这仅仅是为了iOS5的。对于iOS 3/4,您将不得不使用自定义视图。

+0

套住的是,我有一个问题,它现在是工作的罚款。 – 2012-10-12 18:03:01

+0

啊哈!最后,我发现你@teriiehina! – Martin 2015-04-27 16:09:05

+0

@Martin我没有隐藏:) – teriiehina 2015-04-27 18:15:42

0

UIBarButton有没有属性相关的更改字体。但是你可以创建一个带有自定义字体的按钮,然后添加到UIBarButton中。它可能会解决你的问题

0

假设你想支持iOS4和更早版本,你最好的选择是使用initWithCustomView:方法创建一个条形按钮,并提供你自己的视图,可以像UIButton那样,你可以轻松地定制字体。

如果您想通过拖放而非编程方式创建按钮,还可以将UIButton拖到Interface Builder中的工具栏或导航栏上。

不幸的是,这意味着你自己创建按钮背景图像。没有办法在iOS5之前自定义标准UIBarButtonItem的字体。

0

您可以创建自定义编程UIView

UIView *buttonItemView = [[UIView alloc] initWithFrame:buttonFrame]; 

然后添加图像,标签或任何你喜欢你的自定义视图:

[buttonItemView addSubview:customImage]; 

[buttonItemView addSubview:customLabel]; 

... 

现在把它放在你的UIBarButtomItem

UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonItemView]; 

最后添加barButtonItem到你的导航栏。

187

准确地说,这是可以做到如下

[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]; 

为了方便起见,这里的迅速实施:

buttonItem.setTitleTextAttributes([ 
     NSFontAttributeName: UIFont(name: "Helvetica-Bold", size: 26.0)!, 
     NSForegroundColorAttributeName: UIColor.greenColor()], 
    forState: UIControlState.Normal) 
+2

应该使用NSFontAttributeName代替UITextAttributeFont,并且NSForegroundColorAttributeName应该在为IOS7开发时代替UITextAttributeTextColor。 – Raz 2014-02-11 10:12:25

+0

谢谢!它工作正常。 – Raja 2017-05-18 11:21:52

87

对于那些有兴趣使用UIAppearance来设计他们的UIBarButtonItem的字体在整个t他的应用程序,它可以使用这行代码来实现:

目标C:

NSDictionary *barButtonAppearanceDict = @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Light" size:12.0], NSForegroundColorAttributeName: [UIColor whiteColor]}; 
[[UIBarButtonItem appearance] setTitleTextAttributes:barButtonAppearanceDict forState:UIControlStateNormal]; 

夫特2。3:

UIBarButtonItem.appearance().setTitleTextAttributes(
[ 
    NSFontAttributeName : UIFont(name: "HelveticaNeue-Light", size: 12)!, 
    NSForegroundColorAttributeName : UIColor.white 
], 
for: .normal) 

夫特3.0/4

UIBarButtonItem.appearance().setTitleTextAttributes(
[ 
    NSFontAttributeName : UIFont(name: "HelveticaNeue-Light", size: 12)!, 
    NSForegroundColorAttributeName : UIColor.white, 
], for: .normal) 

或单个的UIBarButtonItem(不适用于所有应用宽),如果有用于在特定的一个按钮自定义字体:

let barButtonItem = UIBarButton() 
barButtonItem.setTitleTextAttributes([ 
    NSFontAttributeName : UIFont(name: "FontAwesome", size: 26)!, 
    NSForegroundColorAttributeName : UIColor.white, 
], for: .normal) 
barButtonItem.title = "\u{f02a}" 

当然,你可以改变字体大小&任何你想要的。我更愿意将此代码放在didFinishLaunchingWithOptions部分的AppDelegate.m文件中。

可用属性(只是把它们添加到NSDictionary):

  • NSFontAttributeName:更改字体与UIFont
  • NSForegroundColorAttributeName:用UIColor
  • NSShadow更改颜色:添加阴影(见NSShadow class reference)

(已更新为iOS7 +)

+3

从iOS7开始,UITextAttribute ..键不推荐使用。使用NSFontAttribute ...和NSForeground ...等。 – 2014-11-14 12:23:52

+2

我已更新iOS7中引入的弃用。谢谢你们,@EmmanuelAy! – rog 2014-11-15 16:50:56

+0

太棒了!我的应用程序现在看起来很漂亮(以我自己的方式,这是!!:P)谢谢 – Septronic 2015-11-03 23:29:41

7

要做到这一点UIBarButtonItems但不是所有我推荐以下方法。

  1. 创建一个UIBarButtonItem子类。对于所有期望UIBarButtonItems到子类
  2. 你只会用它作为故事板和它的外观代理的自定义类...
  3. 在你的故事板,更改自定义类 - 不要添加任何东西给它AppDelegate中导入UIBarButtonItem子类,下面一行添加到application:didFinishLaunchingWithOptions:

在我来说,我子类UIBarButtonItem为粗体的唯一目的的字符串:

[[BoldBarButtonItem appearance] setTitleTextAttributes: 
    [NSDictionary dictionaryWithObjectsAndKeys: 
    [UIFont boldSystemFontOfSize:18.0], NSFontAttributeName,nil] 
               forState:UIControlStateNormal]; 
16

这些都是上面的很好的答案。刚刚更新了iOS7:

NSDictionary *barButtonAppearanceDict = @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Thin" size:18.0] , NSForegroundColorAttributeName: [UIColor whiteColor]}; 
    [[UIBarButtonItem appearance] setTitleTextAttributes:barButtonAppearanceDict forState:UIControlStateNormal]; 
18

在斯威夫特你这样做如下:

backButtonItem.setTitleTextAttributes([ 
     NSFontAttributeName : UIFont(name: "Helvetica-Bold", size: 26)!, 
     NSForegroundColorAttributeName : UIColor.blackColor()], 
    forState: UIControlState.Normal) 
+1

不要忘记字体后面的感叹号(!)。错误消息:“'_'不能转换为'String'”并不真正有用。 – Ciryon 2015-09-03 06:41:20

15

Swift3

buttonName.setAttributedTitle([ 
     NSFontAttributeName : UIFont.systemFontOfSize(18.0), 
     NSForegroundColorAttributeName : UIColor.red,NSBackgroundColorAttributeName:UIColor.black], 
            forState: UIControlState.Normal) 

迅速

barbutton.setTitleTextAttributes([ 
     NSFontAttributeName : UIFont.systemFontOfSize(18.0), 
     NSForegroundColorAttributeName : UIColor.redColor(),NSBackgroundColorAttributeName:UIColor.blackColor()], 
     forState: UIControlState.Normal) 

Objective-C的

 [ barbutton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
     [UIFont fontWithName:@"Helvetica-Bold" size:20.0], NSFontAttributeName, 
     [UIColor redColor], NSForegroundColorAttributeName,[UIColor blackColor],NSBackgroundColorAttributeName, 
     nil] 
     forState:UIControlStateNormal]; 
1

在整个应用程序:

if let font = UIFont(name: "AvenirNext-DemiBold", size: 15) { 
     UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: font,NSForegroundColorAttributeName:TOOLBAR_TITLE_COLOR], forState: UIControlState.Normal) 

    } 
2

SWIFT 3

barButtonName.setTitleTextAttributes([NSFontAttributeName : UIFont.systemFont(ofSize: 18.0),NSForegroundColorAttributeName : UIColor.white], for: .normal)