iOS 7 中的 titleTextAttributes UIAppearance 字体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18898437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
titleTextAttributes UIAppearance font in iOS 7
提问by LOP_Luke
I am using UIAppearance to apply fonts to UINavigationBar and UIBarButtonItem and I am having problems. I ran this code:
我正在使用 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]);
and the result of that log on iOS 7 is:
在 iOS 7 上登录的结果是:
(null)
Where the result in iOS 6 is:
iOS 6 中的结果是:
{
NSFont = "<UICFFont: 0x1d897a80> font-family: \"My_Font\"; font-weight: normal; font-style: normal; font-size: 17px";
}
I can't find anything in the iOS 7 docs that would indicate that this shouldn't work, has anyone else had this problem?
我在 iOS 7 文档中找不到任何表明这不可行的内容,有没有其他人遇到过这个问题?
Edit 1
编辑 1
I actually have gotten this to work with [UINavigationBar appearance]
the problem was that I was setting the point size to 0 in order to have the font be set to the default navbar/barButtonItem size as described in the NSString UIKit Additions Referencebut this apparently no longer works in iOS 7. Instead, setting the point size to 0 will return the system font.
我实际上已经解决了[UINavigationBar appearance]
这个问题,因为我将点大小设置为 0,以便将字体设置为NSString UIKit Additions Reference 中描述的默认导航栏/barButtonItem 大小,但这显然不再适用于iOS 7。相反,将磅值设置为 0 将返回系统字体。
I am still unable to set titleTextAttributes
to
我仍然无法设置titleTextAttributes
为
[UIBarButtonItem appearanceWhenContaintedIn:[UIToolbar class], nil]]
[UIBarButtonItem appearanceWhenContaintedIn:[UIToolbar class], nil]]
回答by TMilligan
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont
fontWithName:@"YOURFONT" size:14], NSFontAttributeName,
[UIColor whiteColor], NSForegroundColorAttributeName, nil];
[[UINavigationBar appearance] setTitleTextAttributes:attributes];
The key is to use NSFontAttributeName and so forth. I assume they are moving over to using the NS variety for 64-bit compatibility. The above code worked on my iOS7 device.
关键是使用 NSFontAttributeName 等等。我假设他们正在转向使用 NS 品种来实现 64 位兼容性。上面的代码适用于我的 iOS7 设备。
回答by Sergey Grischyov
Following @Alex Zavatone's answer - it could be done nicely in just one line of code:
按照@Alex Zavatone 的回答 - 只需一行代码就可以很好地完成:
self.navBar.titleTextAttributes = @{
NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:20.0],
NSForegroundColorAttributeName: [UIColor redColor]
};
回答by Dmitry Volkov
This worked fine for iOS 7 when I moved it to a viewDidLoad method in my view controller.
当我将它移动到视图控制器中的 viewDidLoad 方法时,这对 iOS 7 工作正常。
[[UIBarButtonItem appearance] setTitleTextAttributes:attributes];
When I tried to do this in AppDelegate it didn't work, though[[UINavigationBar appearance] setTitleTextAttributes:setTitleTextAttributes:attributes];
worked fine in AppDelegate. Also it should go before you create and assign the bar button.
当我尝试在 AppDelegate 中执行此操作时,它不起作用,但[[UINavigationBar appearance] setTitleTextAttributes:setTitleTextAttributes:attributes];
在 AppDelegate 中运行良好。它也应该在您创建和分配条形按钮之前进行。
It also works from initWithCoder method if you create your bar button on Storyboard.
如果您在 Storyboard 上创建条形按钮,它也适用于 initWithCoder 方法。
Update:If you change deployment target to 7.0 then Xcode will provide you with warnings saying that for example UITextAttributeTextColor is deprecated and you should use NSForegroundColorAttributeName or instead of UITextAttributeFont use NSFontAttributeName. After changing attribute constants you can call setTitleTextAttributes: from AppDelegate.
更新:如果您将部署目标更改为 7.0,那么 Xcode 将向您提供警告,例如 UITextAttributeTextColor 已被弃用,您应该使用 NSForegroundColorAttributeName 或使用 NSFontAttributeName 代替 UITextAttributeFont。更改属性常量后,您可以从 AppDelegate 调用 setTitleTextAttributes:。
回答by Alex Zavatone
Here's what I do on iOS-7
这是我在 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;
回答by yndolok
Here it is in Swift:
这是在 Swift 中的:
let font = UIFont(name: "My_Font", size: 17.0)
UINavigationBar.appearance().titleTextAttributes = [
NSForegroundColorAttributeName: UIColor.whiteColor(),
NSFontAttributeName: font!
]
*Note that you have to unwrap the optional UIFont.
*请注意,您必须解开可选的 UIFont。
回答by Will Jenkins
For the UIBarButons use the appearance proxy:
对于 UIBarButons 使用外观代理:
NSDictionary *normalAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"Helvetica Neue" size:fontSize], UITextAttributeFont,
nil];
[[UIBarButtonItem appearance] setTitleTextAttributes:normalAttributes
forState:UIControlStateNormal];
If you want to limit which UIBarButtonItems your style affects use appearanceWhenContainedIn: instead.
如果您想限制您的样式影响哪些 UIBarButtonItems,请改用 AppearanceWhenContainedIn:。
For the UINavigationBar, you can create a UILabel and set it to your navigationItem.titleView:
对于 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;
回答by Josh Adams
Here is how to globally change the nav bar title's text color in Swift 4:
以下是如何在 Swift 4 中全局更改导航栏标题的文本颜色:
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.yellow]