titleTextAttributes iOS 7中的UIAppearance字体

我正在使用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"; } 

我无法find任何iOS 7文档,这将表明这不应该工作,有没有其他人有这个问题?

编辑1

我实际上已经得到这个与[UINavigationBar appearance]的问题是,我设置的大小为0,以便将字体设置为默认的navbar / barButtonItem大小,如NSString UIKit Additions Reference中所述,但这显然不再适用于iOS 7.相反,将点大小设置为0将返回系统字体。

我仍然无法设置titleTextAttributes

[UIBarButtonItem appearanceWhenContaintedIn:[UIToolbar class], nil]]

 NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"YOURFONT" size:14], NSFontAttributeName, [UIColor whiteColor], NSForegroundColorAttributeName, nil]; [[UINavigationBar appearance] setTitleTextAttributes:attributes]; 

关键是使用NSFontAttributeName等等。 我假设他们正在转向使用NS的64位兼容性。 上面的代码在我的iOS7设备上工作。

遵循@Alex Zavatone的答案 – 只需一行代码即可完成:

 self.navBar.titleTextAttributes = @{ NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:20.0], NSForegroundColorAttributeName: [UIColor redColor] }; 

当我把它移动到我的视图控制器中的viewDidLoad方法时,这对iOS 7的工作正常。

 [[UIBarButtonItem appearance] setTitleTextAttributes:attributes]; 

当我尝试在AppDelegate中这样做时,它不起作用,但是[[UINavigationBar appearance] setTitleTextAttributes:setTitleTextAttributes:attributes]; 在AppDelegate中工作得很好。 此外,它应该去之前,你创build和分配栏button。

如果您在Storyboard上创build了条形button,它也可以从initWithCoder方法中使用。

更新:如果您将部署目标更改为7.0,则Xcode将向您提供警告,说明例如UITextAttributeTextColor已弃用,您应该使用NSForegroundColorAttributeName或使用NSFontAttributeName而不是UITextAttributeFont。 更改属性常量后,可以从AppDelegate调用setTitleTextAttributes:

这就是我在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; 

这是在斯威夫特:

 let font = UIFont(name: "My_Font", size: 17.0) UINavigationBar.appearance().titleTextAttributes = [ NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: font! ] 

*请注意,您必须打开可选的UIFont。

对于UIBarButons使用外观代理 :

  NSDictionary *normalAttributes = [NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"Helvetica Neue" size:fontSize], UITextAttributeFont, nil]; [[UIBarButtonItem appearance] setTitleTextAttributes:normalAttributes forState:UIControlStateNormal]; 

如果你想限制哪个UIBarButtonItems你的风格影响使用appearanceWhenContainedIn:取而代之。

对于UINavigationBar,您可以创build一个UILabel并将其设置为您的navigationItem.titleView:

 UIabel *title = [[UILabel alloc] initWithFrame:CGRectZero]; title.backgroundColor = [UIColor clearColor]; title.font = [*yourfont*]; title.textColor = [*your color*]; self.navigationItem.titleView = title; 

以下是如何在Swift 4中全局更改导航栏标题的文字颜色:

 UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.yellow]