iPhone UINavigationBar使用[UINavigationBar外观]更改所有控制器的字体样式

Ale*_*one 6 iphone objective-c uinavigationbar ios

我知道我可以单独更改导航栏的字体,如下面的答案所示:更改导航栏的字体

目前我正在使用更全球化的方法:

//in my app delegate:
    [[UINavigationBar appearance] setBarStyle:UIBarStyleBlackTranslucent];
Run Code Online (Sandbox Code Playgroud)

有没有办法全局更改导航栏通过外观对象的字体?

谢谢!

Tig*_*ing 23

来自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]];
Run Code Online (Sandbox Code Playgroud)

  • 建议使用字典文字@ {key1:object1,key2:object2,...}以获得更好的代码可读性. (9认同)

Ros*_*oss 19

@Javy回答@ Philip007的建议:

[[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.0f, 1.0f)],
                                 UITextAttributeFont: [UIFont fontWithName:@"Helvetica-Light" size:0.0f]
 }];
Run Code Online (Sandbox Code Playgroud)

啊......那更好!


run*_*mad 6

以上答案包含已弃用密钥的更新和使用NSShadow:

NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor blackColor];
shadow.shadowBlurRadius = 0.0;
shadow.shadowOffset = CGSizeMake(0.0, 2.0);
[[UINavigationBar appearance] setTitleTextAttributes: @{
                     NSForegroundColorAttributeName : [UIColor blackColor],
                                NSFontAttributeName : [UIFont fontWithName:@"Helvetica-Light" size:0.0f],
                              NSShadowAttributeName : shadow
}];
Run Code Online (Sandbox Code Playgroud)

同时将字体大小设置为0,以便根据导航栏方向/高度自动调整大小.