iOS 7中的titleTextAttributes UIAppearance字体是什么?

30

我正在使用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大小,如NSString UIKit Additions Reference所述,但是这在iOS 7中显然不起作用。 相反,将点大小设置为0将返回系统字体。

我仍然无法将titleTextAttributes设置为

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


你找到解决方法了吗? - iosMentalist
不,从iOS 7.1开始,使用[UIBarButtonItem appearance]仍然存在问题。无论是将字典键更改为NS前缀还是将调用移动到-viewDidLoad中都不能解决问题。只要字体的大小大于0,[UINavigationBar appearance]就可以正常工作。 - LOP_Luke
7个回答

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

[[UINavigationBar appearance] setTitleTextAttributes:attributes];

关键是使用NSFontAttributeName等等。我认为他们正在转向使用NS变量以实现64位兼容性。上述代码在我的iOS7设备上运行良好。


啊,这很有道理。不过 UIBarButtonItem 怎么样呢?当我尝试将 titleTextAttributes 设置为 [UIBarButtonItem appearance] 时,它似乎被忽略了。你能让外观代理与 UIBarButtonItem 一起工作吗?另外感谢你的帮助。 - LOP_Luke
2
iOS7导航栏后退按钮的默认字体大小是多少? - Bharat
这节省了我很多时间!谢谢!默认字体的名称是什么?我想匹配默认字体,但使用蓝色来匹配iOS7中的蓝色UI元素。 - Jackson

18

根据 @Alex Zavatone 的回答,这可以在一行代码内轻松完成:

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

2
好的。很高兴能够帮助到你。 - Alex Zavatone
这对我来说比使用[UINavigationBar appearance]代理更有效。 - manderson

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:。


iOS7导航栏返回按钮的默认字体大小是多少? - Bharat
当我读到这个时,我就像:“嗯,我不想把它添加到所有的viewDidLoads中”,这只是两个超类。在我将其添加到其中一个后,我运行了应用程序,它适用于两个...但不适用于-[AppDelegate applicationDidFinishLaunching:withOptions:]...毫无头绪。 - Mazyod
请注意,现在是这样的:[[UIBarButtonItem appearance] setTitleTextAttributes: attributes forState:<#(UIControlState)#>]; - Reefwing

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;

8

这是用Swift编写的代码:

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

*请注意,您需要取消可选UIFont的封装。


5

对于UIBarButons,使用外观代理

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

如果你想限制你的样式影响哪些UIBarButtonItems,可以使用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;

外观代理对我也不起作用。你真的能够通过这样做改变字体吗?另外,我知道UILabel的技巧,但是现在在iOS 7中改变字体的唯一方法吗?在iOS 6中,我可以使用titleTextAttributes来更改字体。 - LOP_Luke
1
是的,我已经让外观代理起作用了,但确实需要一些技巧。我必须使用appearanceWhenContainedIn并进行两个级别的包含 - 即[[UIBarButtonItem appearanceWhenContainedIn:[MyNavigationBar class],[MyNavigationController class],nil] ... - Will Jenkins
注意 - 包含顺序与我预期的相反(至少如此)... 它从内部到外部。 - Will Jenkins
关于UILabel,不确定它是唯一的方法,但它已经在iOS 6中实现并且仍适用于7。 - Will Jenkins

1

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接