19

I have used this code to change a NavigationItem’s title:

self.navigationItem.title = @"My Name is ABC";

How can I change the font size and font as well?

1
  • 1
    This is super easy now with UIAppearance, see my answer for example code
    – Sneakyness
    Jan 2, 2013 at 21:02

10 Answers 10

32

Sample Code :

CGRect frame = CGRectMake(0, 0, 400, 44);
UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:8.0];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.text = @"Sample custom Title With small Fonts ";
self.navigationItem.titleView = label;
2
  • That's great... But: If you have a Back button on the left, the view is shifted to the right. Is there anything that can be done to prevent this? Thanks!
    – jowie
    Jan 12, 2011 at 16:18
  • We need to add rightbar pr leftbar button item with nil.. to balance.. If we have rightbarbuttonItem add nil button at leftbarbutton and vice versa... Jul 11, 2011 at 4:37
18

As of iOS5+, you can use the UIAppearance protocol to change the font of the nav title, here's code:

NSMutableDictionary *titleBarAttributes = [NSMutableDictionary dictionaryWithDictionary: [[UINavigationBar appearance] titleTextAttributes]];
[titleBarAttributes setValue:[UIFont fontWithName:@"Helvetica-Bold" size:18] forKey:UITextAttributeFont];
[[UINavigationBar appearance] setTitleTextAttributes:titleBarAttributes];
10

Since iOS5 you can use the new titleTextAttributes property like so:

NSMutableDictionary *newAttributes = [[NSMutableDictionary alloc] init]; 
[newAttributes setObject:[UIFont boldSystemFontOfSize:18] forKey:UITextAttributeFont];
[self.navigationController.navigationBar setTitleTextAttributes:newAttributes];

Possible keys are:

UITextAttributeFont
UITextAttributeTextColor
UITextAttributeTextShadowColor
UITextAttributeTextShadowOffset
2
  • I prefer this solution to the others. I have a UITableViewController titled e.g. 'News' that pushes a UIViewController within the same UINavigationController. When the 2nd view controller is displayed the back-button read 'Back' and not 'News' as you would expect. But with this solution the back-button reads 'News'. Good work, Brabbeldas!
    – kjoelbro
    Apr 23, 2013 at 13:40
  • How to make title font to adjust to the width?
    – kidsid49
    Apr 30, 2015 at 20:55
6

To make the above example centered you have to have the size be only as wide as the text, this should do the trick

UIFont * font = [UIFont fontWithName:@"Helvetica" size:18.0f];
CGRect frame = CGRectMake(0, 0, [@"Lorem Ipsum" sizeWithFont:font].width, 44);
4

As of iOS5, you can do this in 1 line:

[[UINavigationBar appearance] setTitleTextAttributes: @{UITextAttributeFont: [UIFont fontWithName:@"Helvetica" size:20.0f]}];
1
  • As a bonus, if you need to adjust the vertical alignment of the custom font, you can add another line as so: [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:-3 forBarMetrics:UIBarMetricsDefault]; Aug 29, 2013 at 4:29
3

Swift 4

in AppDelegate.swift

UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "Helvetica-Bold", size: 15)!], for: .normal)
1

Here is the modern Swift 2 solution:

let customFont = UIFont(name: "Helvetica", size: 17)
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName : customFont!]
1
  • 1
    @jxmorris12 you run in in AppDelegate? works on my app
    – bogen
    Aug 2, 2016 at 13:11
1

//Appearance setup for navigation title font throught application...

let customFont = UIFont(name: "Helvetica-Bold", size: 15)
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.font : customFont!]
0

I have found some good answer for this .I think it's similar to the accepted answer however please find it in some more detail.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {  

   self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    

   if (self){
     // this will appear as the title in the navigation bar    
     UILabel *label = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];      
    label.backgroundColor = [UIColor clearColor];         
    label.font = [UIFont boldSystemFontOfSize:20.0];       
    label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];       
    label.textAlignment = UITextAlignmentCenter;      
    label.textColor = [UIColor yellowColor]; // change this color      
    self.navigationItem.titleView = label;          
    label.text = NSLocalizedString(@"PageThreeTitle", @"");       
    [label sizeToFit];    
 }    
 return self;

} 

For more details, Please refer this Link

0

For swift 3

self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName :UIfont(named:"test") ]
1
  • You have a single bracket. It is not possible.
    – Nuageux
    May 18, 2017 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.