ios 更改 UIBarButtonItem 的字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8849913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:16:36  来源:igfitidea点击:

Change the font of a UIBarButtonItem

ioscocoa-touchuibarbuttonitem

提问by Ankit Vyas

UIToolbar with UIBarButtonItem

带有 UIBarButtonItem 的 UIToolbar

I have a UIBarButtonItemin my UIToolbartitled Done. Now I want to change the font from the default to "Trebuchet MS"with Bold. How can I do that?

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

采纳答案by teriiehina

Because UIBarButtonItem inherits from UIBarItem, you can try

因为 UIBarButtonItem 继承自 UIBarItem,你可以试试

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

but this is for iOS5 only. For iOS 3/4, you will have to use a custom view.

但这仅适用于 iOS5。对于 iOS 3/4,您必须使用自定义视图。

回答by mask

To be precise, this can be done as below

准确地说,这可以按如下方式完成

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

Or with object literal syntax:

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

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

For convenience, here's the Swift implementation:

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

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

回答by rog

For those interested in using UIAppearanceto style their UIBarButtonItem's fonts throughout the app, it can be accomplished using this line of code:

对于那些有兴趣在整个应用程序中使用UIAppearanceUIBarButtonItem字体样式的人,可以使用以下代码行来完成:

Objective C:

目标 C:

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

Swift 2.3:

斯威夫特 2.3:

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

Swift 3

斯威夫特 3

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

Swift 4

斯威夫特 4

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

Or for a single UIBarButtonItem (not for all app wide), if you have a custom font for one button in particular:

或者对于单个 UIBarButtonItem(不适用于所有应用程序范围),如果您有一个按钮的自定义字体,特别是:

Swift 3

斯威夫特 3

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

Swift 4

斯威夫特 4

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

Of course, you can change the font & size to whatever you'd like. I prefer to put this code in the AppDelegate.mfile in the didFinishLaunchingWithOptionssection.

当然,您可以将字体和大小更改为您喜欢的任何内容。我更喜欢将此代码放在AppDelegate.m文件中的didFinishLaunchingWithOptions部分。

Available attributes (just add them to the NSDictionary):

可用属性(只需将它们添加到NSDictionary):

  • NSFontAttributeName: Change font with a UIFont
  • NSForegroundColorAttributeName: Change color with a UIColor
  • NSShadow: Add a drop shadow (see NSShadowclass reference)
  • NSFontAttributeName: 改变字体 UIFont
  • NSForegroundColorAttributeName: 改变颜色 UIColor
  • NSShadow:添加阴影(参见NSShadow类参考)

(Updated for iOS7+)

(针对 iOS7+ 更新)

回答by Antoine

In Swift you would do this as followed:

在 Swift 中,您可以按如下方式执行此操作:

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

回答by David DelMonte

These are great answers above. Just updating for iOS7:

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

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

回答by Anbu.Karthik

Swift3

斯威夫特3

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

swift

迅速

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

Objective-C

目标-C

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

回答by Kyle Clegg

To do this for some UIBarButtonItemsbut not all I recommend the following approach.

要为某些UIBarButtonItems但不是全部执行此操作,我建议使用以下方法。

  1. Create a UIBarButtonItemsubclass. Don't add anything to it - you will only use it as a custom class in the storyboard and for its appearance proxy...
  2. In your storyboard, change the custom class for all desired UIBarButtonItemsto your subclass
  3. In your AppDelegate import your UIBarButtonItemsubclass and add the following line to application:didFinishLaunchingWithOptions:
  1. 创建一个UIBarButtonItem子类。不要向其中添加任何内容 - 您只会将其用作故事板中的自定义类及其外观代理...
  2. 在您的故事板中,将所有所需的自定义类更改UIBarButtonItems为您的子类
  3. 在您的 AppDelegate 中导入您的UIBarButtonItem子类并将以下行添加到application:didFinishLaunchingWithOptions:

In my case I subclassed UIBarButtonItemfor the sole purpose of bolding the text:

在我的情况下,我子类化UIBarButtonItem的唯一目的是将文本加粗:

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

回答by Vinoth Vino

In Swift 4, you can change the font and colour of UIBarButtonItemby adding the following code.

Swift 4 中,您可以UIBarButtonItem通过添加以下代码来更改 的字体和颜色。

addTodoBarButton.setTitleTextAttributes(
        [
        NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Bold", size: 17)!,
        NSAttributedStringKey.foregroundColor: UIColor.black
        ], for: .normal)

回答by Fabio

This is the right way: declare your barButtonItem (in this case rightBarButtonItem) and add it setTitleTextAttributes.

这是正确的方法:声明您的 barButtonItem(在本例中为 rightBarButtonItem)并添加它 setTitleTextAttributes。

navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Go!", style: .plain, target: self, action: #selector(yourFuncDestination))

after you can add title attributes

在您可以添加标题属性之后

navigationItem.rightBarButtonItem?.setTitleTextAttributes([.font : UIFont.systemFont(ofSize: 18, weight: .bold), .foregroundColor : UIColor.white], for: .normal)

you can change the size, the weight (.bold, .heavy, .regular etc.) and the color how you prefer... Hope this help :)

您可以更改大小、重量(.bold、.heavy、.regular 等)和您喜欢的颜色...希望这有助于:)

回答by pankaj nigam

Swift 5 Implementation

Swift 5 实现

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