【发布时间】: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