Change the font of the back navigation button

I want to set the font of the navigation button on the application’s navigation bar without doing anything crazy and without losing any other design characteristics of the button (i.e., I want to keep the arrow).

Right now I'm using this in viewDidAppear: to set the font of the elements of a regular panel.

 for (NSObject *view in self.navigationController.navigationBar.subviews) { if ([view isKindOfClass:[UIButton class]]) { [((UIButton*)view).titleLabel setFont:[UIFont fontWithName:@"Gill Sans" size:14.0]]; } } 

However, this does not make any changes to the back button, no matter which UIViewController this code applies to (root, current, etc.).

+45
ios objective-c uifont uinavigationbar uibarbuttonitem
May 14 '13 at 5:06
source share
7 answers

To change the appearance of text in all UIBarButtonItems displayed in all UIBarButtonItems UINavigationBars , follow these steps in the application:didFinishLaunchingWithOptions:

 [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes: @{UITextAttributeTextColor:[UIColor blackColor], UITextAttributeTextShadowOffset:[NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowColor:[UIColor whiteColor], UITextAttributeFont:[UIFont boldSystemFontOfSize:12.0] } forState:UIControlStateNormal]; 

UPDATE: iOS7 friendly version

 NSShadow *shadow = [[NSShadow alloc] init]; shadow.shadowOffset = CGSizeMake(0.0, 1.0); shadow.shadowColor = [UIColor whiteColor]; [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes: @{NSForegroundColorAttributeName:[UIColor blackColor], NSShadowAttributeName:shadow, NSFontAttributeName:[UIFont boldSystemFontOfSize:12.0] } forState:UIControlStateNormal]; 

Swift:

NOTE: this changes ALL instances of the UIBarButtonItem , not just those contained in the UINavigationBar

 UIBarButtonItem.appearance() .setTitleTextAttributes([NSFontAttributeName : ExamplesDefaults.fontWithSize(22)], forState: UIControlState.Normal) 

Swift3:

 UIBarButtonItem.appearance() .setTitleTextAttributes([NSFontAttributeName: UIFont(name: "FontName-Regular", size: 14.0)!], for: .normal) 
+111
May 14 '13 at 8:40
source share

For those who have not received this fully, here is how I did it, including returning to the Root ViewController in iOS7:

 UIBarButtonItem *backBtn =[[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStyleDone target:self action:@selector(popToRoot:)]; backBtn.title = @"Back"; [backBtn setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"Chalkduster" size:15], NSFontAttributeName, [UIColor yellowColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal]; self.navigationItem.leftBarButtonItem=backBtn; 

popToRoot ViewController:

 - (IBAction)popToRoot:(UIBarButtonItem*)sender { [self.navigationController popToRootViewControllerAnimated:YES]; } 

Maybe someone can use this.

+6
Oct 16 '13 at 15:02
source share

A quick version of all of the above (excerpt from the original answer ):

 let customFont = UIFont(name: "customFontName", size: 17.0)! UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: customFont], forState: .normal) 

Other advantages:
stack overflow

+4
Feb 05 '15 at 15:13
source share

In Swift3:

 let font = UIFont(name: "Verdana", size: 10.0) // Back button UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: font!], for: UIControlState.normal) // Title in the navigation item let fontAttributes = [NSFontAttributeName: font] self.navigationController?.navigationBar.titleTextAttributes = fontAttributes 

Note: you only need to do this once for the navigation controller.

+2
Jan 26 '17 at 15:09 on
source share

Swift 3.0+

AppDelegate.swift

 UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "myFont", size: 17.0)!], for: .normal) 
+2
Jan 30 '18 at 20:07
source share

Use this instead in AppDelegate or where the NavigationController is initialized, the method is available in iOS 5 and later.

 UIBarButtonItem *backbutton = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStyleBordered target:nil action:nil]; [backbutton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor blackColor],UITextAttributeTextColor,[UIFont fontWithName:TEXTFONT size:16.0f],UITextAttributeFont, nil] forState:UIControlStateNormal]; 
+1
May 14 '13 at 5:09
source share

If you use the new UISplitViewControllerDelegate for split views in iOS 8, the above methods will not work, because the new displayModeButtonItem works a little differently.

You need to set the font when creating displayModeButtonItem . Assuming you are following Apple patterns, this is probably in prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender , where you do something like this:

 // From Apple Template: NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath]; DetailViewController *controller = (DetailViewController *)[[segue destinationViewController] topViewController]; [controller setDetailItem:object]; controller.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem; controller.navigationItem.leftItemsSupplementBackButton = YES; // New Line to set the font: [controller.navigationItem.leftBarButtonItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"SourceSansPro-Regular" size:14]} forState:UIControlStateNormal]; 
0
Nov 09 '14 at 6:49
source share



All Articles