Default Font for UINavigationBar Large Title in iOS 11?

Viewed 14109

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);
7 Answers

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.

Edit: I opened the User Interface inspector and it says:

enter image description here

No need to code here :)

On iOS 13 this is the exact font that is used for large titles in navigation bars. I compared screenshots pixel by pixel and it matches exactly!

UIFont.systemFont(ofSize: 34, weight: .bold)

iOS12 Large Title System Bold 34 (San Francisco)

[UIFont boldSystemFontOfSize:34]

iOS9 - iOS12 System Semibold 17 (San Francisco)

[UIFont systemFontOfSize:17 weight:UIFontWeightSemibold]

iOS8 System Semibold 17 (Helvetica Neue)

[UIFont systemFontOfSize:17 weight:UIFontWeightSemibold]

iOS7 System Bold 17 (Helvetica Neue)

[UIFont boldSystemFontOfSize:17];

Try something like this:

if #available(iOS 11.0, *) { navigationBar.largeTitleTextAttributes = [.foregroundColor: UIColor.white, .font: UIFont(name: "HelveticaNeue-Bold", size: 20)!] }

Easiest way is to use

UIFont.preferredFont(forTextStyle: .largeTitle)

Keep in mind, it's not UINavigationBar's large title, it's a style to use in your own views, that's why the weight is off. But you can apply intended weight to it.

UIFont(descriptor: normalTitleFont.fontDescriptor.withSymbolicTraits(.traitBold)!, size: normalTitleFont.pointSize)

Full code:

let normalTitleFont = UIFont.preferredFont(forTextStyle: .largeTitle)
let largeTitleFont = UIFont(descriptor: normalTitleFont.fontDescriptor.withSymbolicTraits(.traitBold)!, size: normalTitleFont.pointSize)

This approach is better compared to hardcoding sizes or fonts.

User can change accessibility settings on device and hardcoded values will be off.

The value of .largeTitle text style will change as well, and your title will get the the right style.

I actually found the answer by trying different fonts and sizes. Now, a disclaimer, this may change with accessibility/dynamic font sizes. But the font and size I found was

[UIFont systemFontOfSize:32 weight:UIFontWeightBold]

I'd still like to find a way to get this programmatically and will wait to accept an answer in the hopes someone might know how to do that. In the meantime, hope this will be helpful to someone.

Related