更改UINavigationController标题的字体

Chr*_*ler 26 fonts title uinavigationcontroller ios

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

mor*_*des 73

从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];
Run Code Online (Sandbox Code Playgroud)

要设置后退按钮的字体,请执行以下操作:

  NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary: [[UIBarButtonItem appearance] titleTextAttributesForState:UIControlStateNormal]];
  [attributes setValue:[UIFont fontWithName:@"Didot" size:12] forKey:NSFontAttributeName];
  [[UIBarButtonItem appearance] setTitleTextAttributes:attributes forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)

要设置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];
}
Run Code Online (Sandbox Code Playgroud)

  • iOS 7弃用了密钥:UITextAttributeFont(读取,不要使用它),现在使用密钥:NSFontAttributeName.工作奇迹,使我的应用程序看起来好多了.谢谢! (4认同)

Mac*_*wia 24

对于iOS8 +,您可以使用:

[self.navigationController.navigationBar setTitleTextAttributes:@{ NSFontAttributeName: [UIFont fontWithName:@"MyFont" size:18.0f],
                                                                   NSForegroundColorAttributeName: [UIColor whiteColor]
                                                                   }];
Run Code Online (Sandbox Code Playgroud)

  • 谢谢这给了我使用swift实现自定义字体的正确方向:self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName:UIFont(name:"MyFont",size:18.0)!] (4认同)
  • 在首先尝试所有上述答案时,总是很难过才发现最底层的答案是最现代的答案: (2认同)

Eri*_*rik 13

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


Jon*_*att 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;
}
Run Code Online (Sandbox Code Playgroud)

  • 将此代码放在viewDidLoad中更好,否则每次在层次结构中出现视图时,都会创建titleView并在系统上执行不必要的工作 (4认同)