2010-10-30 152 views

回答

14

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

+0

这是一个伟大的想法,但我现在有一个问题,.. 当我加我的UILabel我对UINavigationController的控件(如Add按钮)不再可见,.. * HM * – 2010-10-30 18:11:14

+0

我固定它,用自己..对不起一个missunderstood;) – 2010-10-30 18:18:15

+1

如果这解决了问题,不要忘记接受的答案 – Erik 2010-10-31 17:15:19

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; 
} 
+0

+1此提供的解决方案。如果在堆栈viewControllers需要不同的字体的标题,然后'[UINavigationBar的外观]'不能做的工作。自定义titleView是唯一的方法。 – BabyPanda 2012-10-19 04:15:19

+4

它更好地把这段代码放在viewDidLoad中,否则每次查看都会在层次结构中出现,titleView将会被创建并且它在系统上不必要的工作 – raw3d 2013-10-06 13:30:54

71

作为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]; 
+4

iOS 7弃用了键值:UITextAttributeFont(读取,不用这个)现在使用键:NSFontAttributeName来代替。工程奇迹,使我的应用程序看起来好多了。谢谢! – 2013-10-03 16:30:04

+1

在每个单独的视图控制器中,您还可以在当前导航栏的实例上设置标题文本属性以自定义其外观,如下面的代码片段所示:[self.navigationController.navigationBar setTitleTextAttributes:@ {NSFontAttributeName:[UIFont fontWithName:@“帕拉提诺-粗体”尺寸:28],NSForegroundColorAttributeName:的UIColor whiteColor]}]; – CodePlumber 2015-03-21 16:53:23

18

为iOS8上的+,你可以使用:

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

感谢这给了我正确的方向来实现自定义字体与swift:self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName:UIFont(名称:“MyFont”,大小:18.0)!] – NBoymanns 2016-03-15 08:10:43

+0

谢谢,队友!解决了它。 – Felipe 2016-04-14 17:58:14

+2

总是伤心尝试首先上面的答案时,只是找出一个底部是最现代的一个\: – hashier 2016-11-30 12:29:29

1

从@morgancodes答案将设置所有的字体UINavigationController标题。我已经更新了它的雨燕4:

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