Question

How do I get the UIFont of the title in UINavigationController? I attempted to read it like so:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Some text here";
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSLog(@"navigation bar title attributes = %@", self.navigationController.navigationBar.titleTextAttributes);
    NSLog(@"title font = %@", [self.navigationController.navigationBar.titleTextAttributes objectForKey:UITextAttributeFont]);
}

The output was disappointing. The titleTextAttributes dictionary only contained one key/value, which was not the font.

navigation bar title attributes = {
    NSColor = "UIDeviceWhiteColorSpace 1 1";
}
title font = (null)
Was it helpful?

Solution

You can access the subviews of navigation bar to get the titlelable and get font

for (UIView *sv in self.navigationController.navigationBar.subviews)
{
    if (sv.subviews.count > 0 && [sv.subviews[0] isKindOfClass:[UILabel class]])
    {
        UILabel *lbl = (UILabel *)sv.subviews[0];
        NSLog(@"%@",lbl.font);
        break;
    }
}

Console output

<UICTFont: 0xd8a3e50> font-family: "Helvetica-Light"; font-weight: normal; font-style: normal; font-size: 16.00pt

OTHER TIPS

You are on the right track. But keep in mind that there is a default font used if no font has been set for the specific UINavigationBar or through the UIAppearance proxy.

To determine the default font if you don't find a specific one, use the UIFont class methods systemFontOfSize: and systemFontSize.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top