Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change font of back navigation bar button

I want to be able to set the font of my apps navigation bar back button without doing anything too crazy and without losing any other design characteristics of the button (i.e. I want to keep the arrow).

Right now I use this in viewDidAppear: to set the font of the normal bar button items.

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 makes no change on the back button, regardless of which UIViewController this code is applied to (root, current, etc.).

like image 945
zachjs Avatar asked May 14 '13 05:05

zachjs


5 Answers

To change the appearance of the text in all UIBarButtonItems appearing in all UINavigationBars, do the following in 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 UIBarButtonItem, not just those contained within a 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) 
like image 71
Mike Pollard Avatar answered Nov 14 '22 11:11

Mike Pollard


For anyone that did not fully got this to work, here is how i did it, including popped back 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 may have use of this.

like image 45
PeterK Avatar answered Nov 14 '22 11:11

PeterK


Swift version of the all mentioned above (excerpt from the original answer) :

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

More goodies:
https://stackoverflow.com/a/28347428/469614

like image 37
Vexy Avatar answered Nov 14 '22 09:11

Vexy


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 have to do this once for the Navigation controller.

like image 2
Vincent Avatar answered Nov 14 '22 09:11

Vincent


Swift 3.0+

AppDelegate.swift

UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "myFont", size: 17.0)!], for: .normal)
like image 2
smohn Avatar answered Nov 14 '22 10:11

smohn