CaMenz
CaMenz

Reputation: 121

Navigation Bar Title - Change Font without changing size

I'm having trouble to change both the regular and large titles of my Navigation Bar. I only want to change the font, without interfering on the font size. I tried getting the pointSize, but it's not available for NavBar titles. How can I do it?

Upvotes: 0

Views: 530

Answers (2)

Dheeraj
Dheeraj

Reputation: 125

Well There is a way but not programmatically. You can manually set the different font sizes from the utility panes in attribute inspector.

][

Upvotes: 0

Tung Fam
Tung Fam

Reputation: 8147

There is no way to change only font size. Since the font represents both size and the font itself.

Solution: change the font (both size and font) but set the default size. If you haven't made any changes to your project regarding the size then it's 17 by default. I tested it out and found it out.

So here is what you can do:

let attributes: [NSAttributedStringKey: Any] = [
        NSAttributedStringKey.font: YourFontOfSize(size: 17.0)
    ]

navigationController?.navigationBar.titleTextAttributes = attributes

Hope it helps! Please let me know if it worked.

UPDATE: Navigation bar also has a property called .largeTitleTextAttributes. So you can do the same as I did for .titleTextAttributes: Find out the default size by just trying to set the system font with sizes: 15, 16, 17 etc. And then when the size matches you will know which is the default size. And then apply your custom font using the default size that you found out.

Upvotes: 2

Related Questions