【问题标题】:Altering font in a programmatic navigation bar (Objc)(Xcode 7.3)在程序化导航栏中更改字体 (Objc)(Xcode 7.3)
【发布时间】:2016-08-11 00:21:23
【问题描述】:

如何更改程序化导航栏(标题)上的字体?我已经搜索了很长一段时间,并且看到了各种各样的代码 - 但没有一个在我的项目中有效。

以下是我尝试过的一些事情:

[[UINavigationBar appearance] setTitleTextAttributes: @{
                            UITextAttributeTextColor: [UIColor     greenColor],
                      UITextAttributeTextShadowColor: [UIColor     redColor],
                     UITextAttributeTextShadowOffset: [NSValue     valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)],
                                 UITextAttributeFont: [UIFont     fontWithName:@"Helvetica" size:20.0f]
 }];

`NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor blackColor];
shadow.shadowBlurRadius = 0.0;
shadow.shadowOffset = CGSizeMake(0.0, 2.0);
[[UINavigationBar appearance] setTitleTextAttributes: @{
                 NSForegroundColorAttributeName : [UIColor blackColor],
                            NSFontAttributeName : [UIFont     fontWithName:@"Helvetica-Light" size:0.0f],
                          NSShadowAttributeName : shadow

}];


[[UINavigationBar appearance] setTitleTextAttributes: 
[NSDictionary dictionaryWithObjectsAndKeys: 
    [UIColor whiteColor], NSForegroundColorAttributeName, 
       [UIFont fontWithName:@"LeagueGothic-Regular" size:16.0], NSFontAttributeName,nil]];

一些背景信息:navigationController 以编程方式设置并在堆栈中保存 4 个视图。对于这个看似简单的问题的任何提示/帮助将不胜感激。

【问题讨论】:

  • 用您尝试过的方法更新您的问题。
  • @rmaddy:谢谢你的建议(我绝对应该从一开始就这样做!)。
  • 使用UINavigationBar appearance 会影响在您调用UINavigationBar appearance 后创建的所有UINavigationBar 实例。因此,请确保在创建导航栏之前调用它。如果您只想影响单个导航栏,请不要使用UINavigationBar appearance。相反,请在特定导航栏实例上调用 setTitleTextAttributes
  • 在创建导航栏之前我有NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys: [UIColor colorWithRed: 44.0/255 green:69.0/255 blue:86.0/255 alpha: 0.90f], NSForegroundColorAttributeName, [UIFont fontWithName:@"Machinato- Light" size: 22], NSFontAttributeName, nil]; 然后[[UINavigationBar appearance]setTitleTextAttributes: textAttributes]; - 没有成功。
  • 对,但这是在创建导航栏之前还是之后?只有在创建导航栏之前调用该代码才有效。

标签: objective-c uinavigationcontroller uinavigationbar xcode7.3


【解决方案1】:

设置titleTextAttributes。没有什么“弃用”的。将其视为 NSAttributedString 的属性,它会正常工作。

例子:

    UINavigationBar.appearance().titleTextAttributes = [
        NSFontAttributeName: UIFont(name:"Chalkduster", size:20)!,
        NSForegroundColorAttributeName: UIColor.black
    ]

结果:

【讨论】:

  • 您好,感谢您的回复^_^ 我一直在使用titleTextAttributes(我已经更新了我的问题)。如果您认为我还遗漏了什么,请告诉我。
  • 嗯,第一个显然是错误的。整个 UITextAttributeFont 都在窗外。这就是为什么我说“将其视为 NSAttributedString 的属性”。我的回答是对的;我不知道你在做什么,不知道为什么你认为它不是。
  • 添加了实际代码和实际结果的屏幕截图。去吧,照样做。
  • 感谢您的示例代码 - 进行比较并确保在我的代码尝试期间没有遗漏任何内容总是很好。
  • 我同意。您的第一步应该始终是创建一个干净的新项目并尝试任何给您带来麻烦的方法,以说服自己它通常可以正常工作。然后,您可以尝试跟踪您在 真实 项目中阻止 工作的原因。如果这对您不起作用,则 做错了其他事情 - 但我无法从这里看到它是什么,因为您提供的信息不足。
【解决方案2】:

根据评论和回答的 SO 用户的指导(感谢 @rmaddy 和 @matt),我找到了一种解决方法。我单独更改了每个视图控制器(VC)的 ViewDidLoad 中的字体(以及颜色和大小)属性,如下所示:

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

此外,我必须在 ViewDidAppear 中为 rootVC 设置导航栏标题 - 当应用程序最初打开时,标题在 ViewDidLoad 中很好,但是当我从另一个 VC 导航到 rootVC 时,标题消失了。

我包含了最后一部分,以防它将来可以帮助其他人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-13
    • 2016-07-19
    • 2011-08-15
    • 2014-12-23
    • 2014-07-21
    • 1970-01-01
    • 1970-01-01
    • 2014-10-12
    相关资源
    最近更新 更多