更改导航栏字体

64

问题非常简单易懂,但是答案却不是那么简单。

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


2
只是因为这是我在谷歌搜索主题时得到的第一个结果,而且有一个更新的答案:https://dev59.com/r17Va4cB1Zd3GeqPGRJQ#10440362 - Matt Mc
我确信这两个问题是有区别的。我问的是如何更改单个导航栏的字体,而另一个问题是关于更改“所有”导航栏的字体。尽管如此,提到这个链接还是很有用的,因为其他人可能会感兴趣! - Joetjah
7
设置个人导航栏标题字体大小的代码:[self.navigationController.navigationBar setTitleTextAttributes:@{UITextAttributeFont:[UIFont fontWithName:<#(NSString *)#> size:<#(CGFloat)#>]}]; - Philip007
1
对于iOS7,你应该使用NSFontAttributeName而不是UITextAttributeFont。 - ishahak
7个回答

158

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

1
请注意,此答案已过时。在iOS 5和6中,有简单的方法来自定义UI元素的外观。只需使用“appearance”代理即可。 - Philip007
如何让这段代码使用系统字体而不是硬编码为Helvetica? - Brabbeldas
做得很漂亮!(@Brabbeldas,我把它换成了另一种字体:P) - Septronic

15
如果你想在Interface Builder中(不需要编写代码)更改字体,下面是在Xcode6中进行操作的步骤:
1.) 在导航控制器场景下找到导航栏视图 enter image description here 2.) 在属性检查器中更改标题字体、颜色和阴影属性。 enter image description here

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/


4

不确定为什么所有答案都包括阴影。添加操作阴影的代码与更改文本字体无关。这两行代码适用于iOS 8.4和Swift

let attributesDictionary = [NSFontAttributeName: UIFont(name: "Helvetica Neue", size: 14)!]
navigationController!.navigationBar.titleTextAttributes = attributesDictionary
titleTextAttributes存储一个字典,该字典将决定导航栏标题的字体、颜色、大小和其他属性。

文本的默认颜色将是UINavigationBartintColor。如果您想要不同的颜色,请设置tintColor或在属性字典中添加一个NSForegroundColorAttributeName属性和值。 - NRitH
如何让此处使用 SystemFont? - Brabbeldas

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

我已经下载了名为timeburner_regular.ttf的字体,所以我编写了上面的代码。它对我起作用了。 - Vin

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接