更改UINavigationController标题的字体

28

我可以更改我的UINavigationController的字体吗? --> 标题

5个回答

74

从iOS 5开始,您可以通过外观代理更改字体。

https://developer.apple.com/documentation/uikit/uiappearance

以下内容将为所有UINavigationControllers设置标题字体。

  NSMutableDictionary *titleBarAttributes = [NSMutableDictionary dictionaryWithDictionary: [[UINavigationBar appearance] titleTextAttributes]];
  [titleBarAttributes setValue:[UIFont fontWithName:@"Didot" size:16] forKey:NSFontAttributeName];
  [[UINavigationBar appearance] setTitleTextAttributes:titleBarAttributes];

要设置返回按钮的字体,请按照以下步骤进行:

  NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary: [[UIBarButtonItem appearance] titleTextAttributesForState:UIControlStateNormal]];
  [attributes setValue:[UIFont fontWithName:@"Didot" size:12] forKey:NSFontAttributeName];
  [[UIBarButtonItem appearance] setTitleTextAttributes:attributes forState:UIControlStateNormal];

若要设置iOS 11+中可用的大标题的字体,请执行以下操作:

if (@available(iOS 11.0, *)) {
    NSMutableDictionary *largeTitleTextAttributes = [NSMutableDictionary dictionaryWithDictionary: [[UINavigationBar appearance] largeTitleTextAttributes]];
    [largeTitleTextAttributes setValue:[UIFont fontWithName:@"Didot" size:32] forKey:NSFontAttributeName];
    [[UINavigationBar appearance] setLargeTitleTextAttributes:largeTitleTextAttributes];
}

4
iOS 7 废弃了键值:UITextAttributeFont(提示,请勿使用此键值),现在使用键值:NSFontAttributeName。这样做非常棒,使我的应用程序看起来更好了许多。谢谢! - Rob R.
1
在每个单独的视图控制器中,您还可以设置当前导航栏实例上的标题文本属性,以自定义外观,就像这段代码片段一样:[self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Palatino-Bold" size:28], NSForegroundColorAttributeName:[UIColor whiteColor]}]; - CodeBrew

26

对于 iOS8 +,您可以使用:

[self.navigationController.navigationBar setTitleTextAttributes:@{ NSFontAttributeName: [UIFont fontWithName:@"MyFont" size:18.0f],
                                                                   NSForegroundColorAttributeName: [UIColor whiteColor]
                                                                   }];

快捷:

self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "MyFont", size: 18.0)!]

4
谢谢,这为我提供了实现使用 Swift 自定义字体的正确方向:self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "MyFont", size: 18.0)!]。 - NBoymanns
谢谢,伙计!问题解决了。 - Felipe
2
总是很难过,当尝试了以上所有答案后才发现最底下的那个是最现代化的: - hashier

15

标题视图可以是任何视图。因此,只需创建一个UILabel或其他类型的视图,在其中更改字体并将该新视图分配给导航项的标题属性。


这是个好主意,但我现在有一个问题,当我添加我的UILabel时,我的控件(如AddButton)在uinavigationcontroller上不再可见了, - Christian 'fuzi' Orgler
我自己解决了,抱歉我误解了 ;) - Christian 'fuzi' Orgler
1
如果这个解决方案修复了问题,请不要忘记接受答案中提供的解决方案。 - Erik

10
一个例子:
-(void) viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    CGRect frame = CGRectMake(0, 0, 400, 44);
    UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
    label.backgroundColor = [UIColor clearColor];
    label.font = [FontHelper fontFor:FontTargetForNavigationHeadings];
    label.textAlignment = UITextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    label.text = self.navigationItem.title;
    // emboss in the same way as the native title
    [label setShadowColor:[UIColor darkGrayColor]];
    [label setShadowOffset:CGSizeMake(0, -0.5)];
    self.navigationItem.titleView = label;
}

如果堆栈中的viewControllers需要不同字体的标题,则[UINavigationBar appearance]无法完成任务。自定义titleView是唯一的方法。 - BabyPanda
5
最好将这段代码放在viewDidLoad方法中,否则每次视图在层级中出现时都会创建titleView,这会对系统造成不必要的负担。 - raw3d

3

@morgancodes的答案将为所有UINavigationController标题设置字体。我已经更新了Swift 4的代码:

let attributes = [NSAttributedStringKey.font: UIFont(name: "Menlo", size: 14) as Any]
UINavigationBar.appearance().titleTextAttributes = attributes
UIBarButtonItem.appearance().setTitleTextAttributes(attributes, for: .normal)

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