Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default Font for UINavigationBar Large Title in iOS 11?

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);
like image 518
SAHM Avatar asked Nov 09 '17 22:11

SAHM


2 Answers

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.

like image 152
Andrei Konstantinov Avatar answered Oct 14 '22 12:10

Andrei Konstantinov


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)
like image 38
Tom van Zummeren Avatar answered Oct 14 '22 10:10

Tom van Zummeren