【问题标题】:Change the navigation bar's font更改导航栏的字体
【发布时间】:2011-08-15 11:29:51
【问题描述】:

这个问题简单明了,可惜答案不是。

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

【问题讨论】:

  • 正因为这是我去谷歌搜索该主题时得到的第一个结果,并且有一个更新的答案:stackoverflow.com/a/10440362/700471
  • 我相信这两个问题是有区别的。在我要求更改单个导航栏的字体的地方,另一个问题是关于更改“所有”导航栏的字体。尽管如此,这是一个有用的链接,其他人可能会感兴趣!
  • 单独的导航栏,使用这个:[self.navigationController.navigationBar setTitleTextAttributes:@{UITextAttributeFont:[UIFont fontWithName:<#(NSString *)#> size:<#(CGFloat)#>]}];
  • 对于 iOS7 你应该使用 NSFontAttributeName 而不是 UITextAttributeFont

标签: iphone objective-c uinavigationcontroller uinavigationbar


【解决方案1】:

不确定为什么所有答案都包含阴影。添加操纵阴影的线条对于更改文本字体没有任何作用。这两行代码适用于 iOS 8.4 和 Swift

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

titleTextAttributes 存储了一个字典,它将指示导航栏标题的字体、颜色、大小和其他属性。

【讨论】:

  • 并且文本的默认颜色将是 UINavigationBartintColor 是什么。如果您想要不同的东西,请设置色调颜色,或将NSForegroundColorAttributeName 属性和值添加到该属性字典。
  • 如何让它使用SystemFont?
【解决方案2】:

如果您想在 Interface Builder 本身中更改字体(无需任何代码),请使用 Xcode6 中的方法:

1.) 在导航控制器场景下找到导航栏视图

2.) 在属性检查器中更改标题字体、颜色和阴影属性。

【讨论】:

    【解决方案3】:
         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的字体,所以我编写了上面的代码。它对我有用。
    【解决方案4】:

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

    【讨论】:

    • 请提醒此答案已过时。在 iOS 5 和 6 中有一些简单的方法可以自定义 UI 元素的外观。只需使用 appearance 代理即可。
    • 如何让这段代码使用 SystemFont 而不是硬编码到 Helvetica?
    • 干得漂亮! (@Brabbeldas 我确实将其更改为另一种字体:P)
    【解决方案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/

    【讨论】:

      【解决方案6】:

      从 iOS 5 开始,您可以使用外观代理。

      答案在这个问题的副本中:https://stackoverflow.com/a/12364740/883413

      【讨论】:

        【解决方案7】:

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

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

        【讨论】:

          猜你喜欢
          • 2018-02-13
          • 2014-07-21
          • 1970-01-01
          • 2017-01-19
          • 2016-06-20
          • 2016-01-28
          • 1970-01-01
          • 2012-01-14
          • 1970-01-01
          相关资源
          最近更新 更多