且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

iOS更改导航栏标题的字体和颜色

更新时间:2022-10-17 17:50:20

改变标题字体)是:
$ b $ pre $ [self.navigationController.navigationBar setTitleTextAttributes:
@ {NSForegroundColorAttributeName:[UIColor redColor],
NSFontAttributeName:[UIFont fontWithName:@mplus-1c-regularsize:21]}];

Swift:

  self.navigationController?.navigationBar.titleTextAttributes = 
[NSForegroundColorAttributeName:UIColor.redColor(),
NSFontAttributeName:UIFont(name:mplus-1c-regular,size:21)! ]


So i have this code that should change the nav bar title font, but it doenst

    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont
                                                                       fontWithName:_dataManager.optionsSettings.fontString size:14], NSFontAttributeName,
                            [UIColor whiteColor], NSForegroundColorAttributeName, nil];

[[UINavigationBar appearance] setTitleTextAttributes:attributes];

Changing the back button font with this code works just fine.

   //set backbutton font
NSDictionary *normalAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                  [UIFont fontWithName:_dataManager.optionsSettings.fontString size:15], NSFontAttributeName,
                                  nil];
[[UIBarButtonItem appearance] setTitleTextAttributes:normalAttributes
                                            forState:UIControlStateNormal];

The correct way to change the title font (and color) is:

[self.navigationController.navigationBar setTitleTextAttributes:
 @{NSForegroundColorAttributeName:[UIColor redColor],
NSFontAttributeName:[UIFont fontWithName:@"mplus-1c-regular" size:21]}];

Swift:

self.navigationController?.navigationBar.titleTextAttributes = 
[NSForegroundColorAttributeName: UIColor.redColor(),
 NSFontAttributeName: UIFont(name: "mplus-1c-regular", size: 21)!]