2011-04-29 139 views
64

这个问题很简单,很简单,答案不幸。更改导航栏的字体

如何更改UINavigationBar中文字的字体?

+2

只是因为这是我得到的第一个结果,当我去谷歌搜索主题,并有一个更新的答案:http://stackoverflow.com/a/10440362/700471 – 2012-10-12 17:59:41

+0

我相信有一个区别两个问题。在我要求更改单个导航栏的字体的地方,另一个问题是关于更改“全部”导航栏的字体。尽管如此,这是一个有用的链接,可以提及其他人可能感兴趣的内容! – Joetjah 2012-10-22 12:07:48

+7

对于单个导航栏,请使用:[self.navigationController。navigationBar setTitleTextAttributes:@ {UITextAttributeFont:[UIFont fontWithName:<#(NSString *)#> size:<#(CGFloat)#>]}];' – Philip007 2012-11-09 11:56:14

回答

157

距离Ios 7和更高:

NSShadow* shadow = [NSShadow new]; 
shadow.shadowOffset = CGSizeMake(0.0f, 1.0f); 
shadow.shadowColor = [UIColor redColor]; 
[[UINavigationBar appearance] setTitleTextAttributes: @{ 
    NSForegroundColorAttributeName: [UIColor greenColor], 
       NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:20.0f], 
       NSShadowAttributeName: shadow 
                 }]; 

距离Ios 5和更高:

[[UINavigationBar appearance] setTitleTextAttributes: @{ 
           UITextAttributeTextColor: [UIColor greenColor], 
          UITextAttributeTextShadowColor: [UIColor redColor], 
         UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)], 
            UITextAttributeFont: [UIFont fontWithName:@"Helvetica" size:20.0f] 
    }]; 

比iOS 5的早些时候:

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 400, 44)]; 
label.backgroundColor = [UIColor clearColor]; 
label.font = [UIFont boldSystemFontOfSize:20.0]; 
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5]; 
label.textAlignment = UITextAlignmentCenter; 
label.textColor =[UIColor whiteColor]; 
label.text=self.title; 
self.navigationItem.titleView = label;  
[label release]; 
+0

当您向下钻取并返回时,消失... – ozba 2012-11-05 11:13:28

+1

请提醒这个答案已经过时。有很多简单的方法可以在iOS 5和6中自定义UI元素的外观。只需使用“外观”代理即可。 – Philip007 2012-11-09 11:59:19

+0

如何让此代码使用SystemFont而不是硬编码到Helvetica? – Brabbeldas 2015-11-01 16:54:20

5

以上答案有效。我会在最后一行之前添加以下行。如果我不这样做,看起来标签如果在左侧有后退按钮但没有右侧按钮,则中心对齐方式不正确。

... 
[self.navigationItem.titleView sizeToFit]; 
[label release]; // not needed if you are using ARC 
5

更新了iOS的7:

[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: 
                 [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName, 
                 shadow, NSShadowAttributeName, 
                 [UIFont fontWithName:@"HelveticaNeue-CondensedBlack" size:21.0], NSFontAttributeName, nil]]; 

礼节:

http://www.appcoda.com/customize-navigation-status-bar-ios-7/

1
 NSShadow *shadow = [NSShadow new]; 
[shadow setShadowColor: [UIColor clearColor]]; 
[shadow setShadowOffset: CGSizeMake(0.0f, 1.0f)]; 

[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
             [UIFont fontWithName:@"TimeBurner" size:27.0f], NSFontAttributeName, 
             [UIColor whiteColor], NSForegroundColorAttributeName, 
             shadow, NSShadowAttributeName,nil]]; 
+0

我已经下载了名为timeburner_regular.ttf的字体,所以我写了上面的代码。它为我工作。 – Vin 2014-03-06 14:06:33

15

如果你想改变Interface Builder本身的字体(没有任何代码)这里是做在Xcode6方式:

1)查找下导航控制器场景 enter image description here

2.导航栏视图)更改标题字体,颜色和阴影的属性属性督察。 enter image description here

4

不知道为什么所有的答案都包含了阴影。添加操纵阴影的线不会改变文字字体。这些2行的代码会为的iOS 8.4工作和夫特

let attributesDictionary = [NSFontAttributeName: UIFont(name: "Helvetica Neue", size: 14)!] 
navigationController!.navigationBar.titleTextAttributes = attributesDictionary 

titleTextAttributes存储的字典,将决定的字体,颜色,大小,和导航栏的标题的其它属性。

+0

而文本的默认颜色将是'UINavigationBar'的'tintColor'。如果您想要不同的东西,请设置色调颜色,或者将“NSForegroundColorAttributeName”属性和值添加到该属性字典中。 – NRitH 2015-08-24 00:44:40

+0

如何让这个使用SystemFont? – Brabbeldas 2015-11-01 16:53:47