Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change font of UINavigationController title

Can I change the font of my UINavigationController? --> title

like image 723
Christian 'fuzi' Orgler Avatar asked Oct 30 '10 16:10

Christian 'fuzi' Orgler


People also ask

How do I change the font on my wordpress title?

Click on My Sites, then click the Customize button next to Themes. Click on Fonts. Select a different font under Headings. That will change the font for the site title and all other headings on the site.

How do I change the font style of the navigation bar title in Swift?

let navigation = UINavigationBar. appearance() let navigationFont = UIFont(name: "Custom_Font_Name", size: 20) let navigationLargeFont = UIFont(name: "Custom_Font_Name", size: 34) //34 is Large Title size by default navigation.

How do I change the font in SwiftUI?

Hold the command key and click the text to bring up a pop-over menu. Choose Show SwiftUI Inspector and then you can edit the text/font properties.


4 Answers

As of iOS 5 you can change the font via the appearance proxy.

https://developer.apple.com/documentation/uikit/uiappearance

The following will set the title font for all UINavigationControllers.

  NSMutableDictionary *titleBarAttributes = [NSMutableDictionary dictionaryWithDictionary: [[UINavigationBar appearance] titleTextAttributes]];
  [titleBarAttributes setValue:[UIFont fontWithName:@"Didot" size:16] forKey:NSFontAttributeName];
  [[UINavigationBar appearance] setTitleTextAttributes:titleBarAttributes];

To set the font for the back button, do this:

  NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary: [[UIBarButtonItem appearance] titleTextAttributesForState:UIControlStateNormal]];
  [attributes setValue:[UIFont fontWithName:@"Didot" size:12] forKey:NSFontAttributeName];
  [[UIBarButtonItem appearance] setTitleTextAttributes:attributes forState:UIControlStateNormal];

To set the font for the large titles available in iOS 11+, do this:

if (@available(iOS 11.0, *)) {
    NSMutableDictionary *largeTitleTextAttributes = [NSMutableDictionary dictionaryWithDictionary: [[UINavigationBar appearance] largeTitleTextAttributes]];
    [largeTitleTextAttributes setValue:[UIFont fontWithName:@"Didot" size:32] forKey:NSFontAttributeName];
    [[UINavigationBar appearance] setLargeTitleTextAttributes:largeTitleTextAttributes];
}
like image 139
morgancodes Avatar answered Oct 16 '22 07:10

morgancodes


for iOS8+ you can use:

[self.navigationController.navigationBar setTitleTextAttributes:@{ NSFontAttributeName: [UIFont fontWithName:@"MyFont" size:18.0f],
                                                                   NSForegroundColorAttributeName: [UIColor whiteColor]
                                                                   }];

Swift:

self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "MyFont", size: 18.0)!]
like image 39
Mobile Developer Avatar answered Oct 16 '22 07:10

Mobile Developer


The title view can be any view. So just create a UILabel or something else where you change the font and assign that new view to the title property of the navigation item.

like image 20
Erik Avatar answered Oct 16 '22 07:10

Erik


An example:

-(void) viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    CGRect frame = CGRectMake(0, 0, 400, 44);
    UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
    label.backgroundColor = [UIColor clearColor];
    label.font = [FontHelper fontFor:FontTargetForNavigationHeadings];
    label.textAlignment = UITextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    label.text = self.navigationItem.title;
    // emboss in the same way as the native title
    [label setShadowColor:[UIColor darkGrayColor]];
    [label setShadowOffset:CGSizeMake(0, -0.5)];
    self.navigationItem.titleView = label;
}
like image 31
Jonathan Moffatt Avatar answered Oct 16 '22 07:10

Jonathan Moffatt