更改后导航栏按钮的字体 [英] Change font of back navigation bar button

查看:168
本文介绍了更改后导航栏按钮的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够设置我的应用程序导航栏后退按钮的字体,而不会做任何太疯狂的事情而不会丢失按钮的任何其他设计特征(即我想保留箭头)。

I want to be able to set the font of my apps navigation bar back button without doing anything too crazy and without losing any other design characteristics of the button (i.e. I want to keep the arrow).

现在我在 viewDidAppear:中使用它来设置普通条形按钮项的字体。

Right now I use this in viewDidAppear: to set the font of the normal bar button items.

for (NSObject *view in self.navigationController.navigationBar.subviews) {
    if ([view isKindOfClass:[UIButton class]]) {
        [((UIButton*)view).titleLabel setFont:[UIFont 
                                 fontWithName:@"Gill Sans" 
                                         size:14.0]];
    }
}

然而这对后退按钮没有任何改变,无论如何其中 UIViewController 此代码适用于(root,current等)。

However this makes no change on the back button, regardless of which UIViewController this code is applied to (root, current, etc.).

推荐答案

要更改所有 UINavigationBars 中出现的所有 UIBarButtonItems 中文本的外观,请执行以下操作: application:didFinishLaunchingWithOptions:

To change the appearance of the text in all UIBarButtonItems appearing in all UINavigationBars, do the following in application:didFinishLaunchingWithOptions:

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:
    @{UITextAttributeTextColor:[UIColor blackColor],
     UITextAttributeTextShadowOffset:[NSValue valueWithUIOffset:UIOffsetMake(0, 1)],
     UITextAttributeTextShadowColor:[UIColor whiteColor],
     UITextAttributeFont:[UIFont boldSystemFontOfSize:12.0]
    }
     forState:UIControlStateNormal];

更新:iOS7友好版

NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowOffset = CGSizeMake(0.0, 1.0);
shadow.shadowColor = [UIColor whiteColor];

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]
 setTitleTextAttributes:
 @{NSForegroundColorAttributeName:[UIColor blackColor],
   NSShadowAttributeName:shadow,
   NSFontAttributeName:[UIFont boldSystemFontOfSize:12.0]
   }
 forState:UIControlStateNormal];

Swift:

注意:此更改 UIBarButtonItem 的所有实例,而不仅仅是 UINavigationBar

NOTE: this changes ALL instances of UIBarButtonItem, not just those contained within a UINavigationBar

UIBarButtonItem.appearance()
               .setTitleTextAttributes([NSFontAttributeName : ExamplesDefaults.fontWithSize(22)], 
                                       forState: UIControlState.Normal)

Swift3:
UIBarButtonItem.appearance()。setTitleTextAttributes([NSFontAttributeName:UIFont(name:FontName-Regular) ,大小:14.0)!],对于:.normal)

Swift3: UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "FontName-Regular", size: 14.0)!], for: .normal)

这篇关于更改后导航栏按钮的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆