更改UINavigationBar字体属性?

Jac*_*cob 43 fonts cocoa-touch objective-c title uinavigationbar

我在UIViewController视图中添加了UINavigationBar.我想更改字体属性.请注意,我想更改UINavigationBar而不是控制器.在我使用UINavigationController的应用程序中,我用它self.navigationItem.titleView = label;来显示自定义标签.

如何在UINavigationBar中获得自定义标题?

PS我用它来设置self.navBar.topItem.title = @"Information";我的UINavigationBar 的标题文本.

小智 89

从iOS 5开始,我们必须使用titleTextAttribute Dictionary(UInavigation控制器类引用中的预定义字典)设置标题文本颜色和导航栏的字体.

[[UINavigationBar appearance] setTitleTextAttributes: 
    [NSDictionary dictionaryWithObjectsAndKeys: 
        [UIColor blackColor], NSForegroundColorAttributeName, 
           [UIFont fontWithName:@"ArialMT" size:16.0], NSFontAttributeName,nil]];
Run Code Online (Sandbox Code Playgroud)

以下教程是UIElements自定义的最佳教程,如UInavigation bar,UIsegmented control,UITabBar.这可能对您有所帮助

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

  • 并且应该用NSForegroundColorAttributeName替换UITextAttributeTextColor (3认同)
  • @rolandjitsu你只需要使用NSFontAttributeName而不是UITextAttributeFont. (2认同)

Tie*_*eme 21

(使用新的iOS 5.0 Appearance API无法实现).

编辑:

iOS> = 5.0:

设置导航栏的标题文本属性:

// Customize the title text for *all* UINavigationBars
NSDictionary *settings = @{
    UITextAttributeFont                 :  [UIFont fontWithName:@"YOURFONTNAME" size:20.0],
    UITextAttributeTextColor            :  [UIColor whiteColor],
    UITextAttributeTextShadowColor      :  [UIColor clearColor],
    UITextAttributeTextShadowOffset     :  [NSValue valueWithUIOffset:UIOffsetZero]};

[[UINavigationBar appearance] setTitleTextAttributes:settings];
Run Code Online (Sandbox Code Playgroud)

iOS <5.0

UINavigationItem没有名为label之类的属性,只有titleView.您只能通过将自定义标签设置为此标题视图来设置字体您可以使用以下代码:(如此处所示)

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
label.font = [UIFont fontWithName:@"YOURFONTNAME" size:20.0];
label.shadowColor = [UIColor clearColor];
label.textColor =[UIColor whiteColor];
label.text = self.title;  
self.navigationItem.titleView = label;      
[label release];
Run Code Online (Sandbox Code Playgroud)


sta*_*kay 7

把它放在你的app委托中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Run Code Online (Sandbox Code Playgroud)

来自Ray Wenderlich:

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

[[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:@"STHeitiSC-Light" size:0.0], 
UITextAttributeFont, nil]];
Run Code Online (Sandbox Code Playgroud)


kar*_*agu 5

在iOS8 swift中,使用以下代码设置字体:

var navigationBarAppearance = UINavigationBar.appearance()
let font = UIFont(name: "Open Sans", size: 17)
if let font = font {
    navigationBarAppearance.titleTextAttributes = [NSFontAttributeName: font, NSForegroundColorAttributeName: UIColor.whiteColor()]
}
Run Code Online (Sandbox Code Playgroud)