Default Font for UINavigationBar Large Title in iOS 11?

SAHM picture SAHM · Nov 9, 2017 · Viewed 13.3k times · Source

I know how to set the color and change the font for the large title in iOS 11, but I am having a hard time finding the default font and font size for the large title. I would like to reproduce the font elsewhere in the app. I have tried the following but they give no results for font or font size. Not sure where else I should be looking for this.

NSLog(@"self.navigationController.navigationBar.largeTitleTextAttributes:%@",self.navigationController.navigationBar.largeTitleTextAttributes);

NSDictionary *dict = self.navigationController.navigationBar.largeTitleTextAttributes;
UIFont *font = [dict objectForKey:@"NSFontAttributeName"];
NSLog(@"font is.. %@, %@, %@",font,font.fontName,font.fontDescriptor);

Answer

rmaddy picture rmaddy · Nov 10, 2017

The proper solution for getting the large title font is the following:

Objective-C:

UIFont *font = [UIFont preferredFontForTextStyle: UIFontTextStyleLargeTitle];
NSLog("%@", font);

Swift:

let font = UIFont.preferredFont(forTextStyle: .largeTitle)
print(font)

This will give the correct value even if the user has applied various accessibility and font size changes in the Settings app.

The above will print out the following by default (in a playground):

<UICTFont: 0x7fa865c05390> font-family: ".SFUIDisplay"; font-weight: normal; font-style: normal; font-size: 34.00pt

Note that the use of .largeTitle in the code above must be called only with iOS 11. If your app supports iOS 10 or earlier, you must wrap that code in a proper use of @available.