我正在使用UIAppearance将字体应用于UINavigationBar和UIBarButtonItem,我遇到了问题。我运行了这段代码:
[[UIBarButtonItem appearanceWhenContainedIn:[UIToolbar class], nil]
setTitleTextAttributes:
@{NSFontAttributeName : [UIFont fontWithName:@"My_Font" size:17.0]}
forState:UIControlStateNormal];
NSLog(@"%@", [[UIBarButtonItem appearanceWhenContainedIn:
[UIToolbar class], nil] titleTextAttributesForState:UIControlStateNormal]);
,iOS 7上的日志结果是:
(null)
iOS 6中的结果是:
{
NSFont = "<UICFFont: 0x1d897a80> font-family: \"My_Font\"; font-weight: normal; font-style: normal; font-size: 17px";
}
我在iOS 7文档中找不到任何表明这不起作用的内容,有其他人有这个问题吗?
修改1
我实际上已经使用[UINavigationBar appearance]
这个问题是我将点大小设置为0,以便将字体设置为默认的navbar / barButtonItem大小,如{{3}中所述但这显然不再适用于iOS 7.相反,将磅值设置为0将返回系统字体。
我仍然无法将titleTextAttributes
设置为
[UIBarButtonItem appearanceWhenContaintedIn:[UIToolbar class], nil]]
答案 0 :(得分:49)
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont
fontWithName:@"YOURFONT" size:14], NSFontAttributeName,
[UIColor whiteColor], NSForegroundColorAttributeName, nil];
[[UINavigationBar appearance] setTitleTextAttributes:attributes];
关键是使用NSFontAttributeName等等。我假设他们正在转向使用NS品种进行64位兼容。上面的代码适用于我的iOS7设备。
答案 1 :(得分:18)
关注@Alex Zavatone的答案 - 只需一行代码即可完成:
self.navBar.titleTextAttributes = @{
NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:20.0],
NSForegroundColorAttributeName: [UIColor redColor]
};
答案 2 :(得分:11)
当我将它移动到视图控制器中的viewDidLoad方法时,这适用于iOS 7。
[[UIBarButtonItem appearance] setTitleTextAttributes:attributes];
当我尝试在AppDelegate中执行此操作时,虽然[[UINavigationBar appearance] setTitleTextAttributes:setTitleTextAttributes:attributes];
在AppDelegate中正常工作,但它无效。此外,它应该在您创建并指定条形按钮之前进行。
如果在Storyboard上创建条形按钮,它也可以从initWithCoder方法开始。
<强>更新强> 如果将部署目标更改为7.0,则Xcode将向您提供警告,例如UITextAttributeTextColor已弃用,您应使用NSForegroundColorAttributeName或使用NSFontAttributeName代替UITextAttributeFont。更改属性常量后,您可以从AppDelegate调用setTitleTextAttributes:
答案 3 :(得分:9)
这是我在iOS-7上做的事情
UIColor *red = [UIColor colorWithRed:165.0f/255.0f green:1.0f/255.0f blue:0.0f/255.0f alpha:1.0];
UIFont *font = [UIFont fontWithName:@"HelveticaNeue-Light" size:24.0f];
NSMutableDictionary *navBarTextAttributes = [NSMutableDictionary dictionaryWithCapacity:1];
[navBarTextAttributes setObject:font forKey:NSFontAttributeName];
[navBarTextAttributes setObject:red forKey:NSForegroundColorAttributeName ];
self.navBar.titleTextAttributes = navBarTextAttributes;
答案 4 :(得分:5)
对于UIBarButons,请使用appearance proxy:
NSDictionary *normalAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"Helvetica Neue" size:fontSize], UITextAttributeFont,
nil];
[[UIBarButtonItem appearance] setTitleTextAttributes:normalAttributes
forState:UIControlStateNormal];
如果你想限制你的风格影响哪个UIBarButtonItem使用appearanceWhenContainedIn:而不是。
对于UINavigationBar,您可以创建UILabel并将其设置为navigationItem.titleView:
UIabel *title = [[UILabel alloc] initWithFrame:CGRectZero];
title.backgroundColor = [UIColor clearColor];
title.font = [*yourfont*];
title.textColor = [*your color*];
self.navigationItem.titleView = title;
答案 5 :(得分:1)
以下是如何在Swift 4中全局更改导航栏标题的文本颜色:
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.yellow]