LOP*_*uke 30 uikit uibarbuttonitem ios uiappearance ios7
我正在使用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]]
TMi*_*gan 49
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont 
    fontWithName:@"YOURFONT" size:14], NSFontAttributeName, 
    [UIColor whiteColor], NSForegroundColorAttributeName, nil];
[[UINavigationBar appearance] setTitleTextAttributes:attributes];
关键是使用NSFontAttributeName等等.我假设他们正在转向使用NS品种进行64位兼容.上面的代码适用于我的iOS7设备.
Ser*_*yov 18
关注@Alex Zavatone的答案 - 只需一行代码即可完成:
self.navBar.titleTextAttributes = @{
    NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:20.0], 
    NSForegroundColorAttributeName: [UIColor redColor]
};
Dmi*_*kov 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 :.
这是我在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;
这是在Swift中:
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];
如果要限制样式影响哪个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;
| 归档时间: | 
 | 
| 查看次数: | 49475 次 | 
| 最近记录: |