如何设置字体和UINavigationBar中使用iOS5外观API的标题颜色? [英] How to set font & color of the title in UINavigationBar using iOS5 appearance API?

查看:148
本文介绍了如何设置字体和UINavigationBar中使用iOS5外观API的标题颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个View控制器,我想将所有字体颜色设置为红色。

I have a multiple View Controllers and i want to set the font color of all to red.

 [[UINavigationBar appearance] setFont:[UIFont boldSystemFontOfSize:12.0]];

抛出无法识别的选择器错误。

is throwing an unrecognized selector error.

请给出一个代码,以帮助解决以下问题。

please give a code, to help fix the following.

推荐答案

来自Ray Wenderlich:

From Ray Wenderlich:

http://www.raywenderlich.com/4344/user- interface-customization-in-ios-5

// Customize the title text for *all* UINavigationBars
[[UINavigationBar appearance] setTitleTextAttributes:
    [NSDictionary dictionaryWithObjectsAndKeys:
        [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0], 
        UITextAttributeTextColor, 
        [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8], 
        UITextAttributeTextShadowColor, 
        [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], 
        UITextAttributeTextShadowOffset, 
        [UIFont fontWithName:@"Arial-Bold" size:0.0], 
        UITextAttributeFont, 
        nil]];

或者如果您更喜欢对象文字样式:

Or if you prefer with the object literal style:

[[UINavigationBar appearance] setTitleTextAttributes:@{
    UITextAttributeTextColor: [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0],
    UITextAttributeTextShadowColor: [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8],
    UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
    UITextAttributeFont: [UIFont fontWithName:@"Arial-Bold" size:0.0],
}];

编辑iOS 7及以下

UITextAttributes弃用为iOS 7,您可以使用以下内容:

UITextAttributes are deprecate as iOS 7 you can use the following :

NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor colorWithWhite:.0f alpha:1.f];
shadow.shadowOffset = CGSizeMake(0, -1);

[[UINavigationBar appearance] setTitleTextAttributes:@{
     NSForegroundColorAttributeName: [UIColor whiteColor],
     NSShadowAttributeName: shadow,
     NSFontAttributeName: [UIFont fontWithName:@"Arial-Bold" size:15.0f]
     }];

这篇关于如何设置字体和UINavigationBar中使用iOS5外观API的标题颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆