Changing UINavigationBar font in Swift
Solution 1
Well, it took me the whole day, but I got it. My first problem was I didn't even have a NavigationController, so once I got that all sorted out this is what finally fixed it. Turns out you call the font like this: Comic Andy
instead of Comic_Andy
. EVEN THOUGH the font is referred to throughout the entire package as Comic_Andy, and the file is named Comic_Andy, you still use Comic Andy.
Solution 2
Instead of using myNavigationBar, try navigationController.navigationBar in the ViewController viewDidLoad function. It worked for me.
navigationController.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Comic_Andy", size: 22)]
If this doesn't work, try using the .ttf suffix.
navigationController.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Comic_Andy.ttf", size: 22)]
Good luck!
Solution 3
Can also be done for all NavigationBars with UINavigationBar.appearance()
.
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name: Font.mediumFontName, size: 20)!]
Related videos on Youtube
Comments
-
dcgoss over 1 year
I have a
UINavigationBar
with a title in the middle. I have added a custom font ("Comic_Andy.ttf
") to my app (I have checkedinfo.plist
to make sure it's listed, and I have checked theCopy Bundle Resources
to make sure it has been added), and I would like the title of theUINavigationBar
to be displayed in that font. From what I can gather it seems as though I'm supposed to use this in myViewController
:myNavigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Comic_Andy", size: 22)]
I placed that method in the
viewDidLoad
function of theViewController
.
I have also tried this in thedidFinishLaunchingWithOptions
function of theAppDelegate
:UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name: "Comic_Andy", size: 22)]
I am programming in Swift, in XCode 6 Beta 6. Many resources regarding this task have mentioned using a method calledsetTitleTextAttributes
, which is nowhere to be seen. I can't figure it out for the life of me - I've probably spent close to 3 hours on it by now - I have checked every StackOverflow answer, every website, so please do not mark this as a duplicate.
Many thanks in advance! -
trumpeter201 over 9 yearsYou should post this as an answer.
-
esreli over 9 yearsthe font name and the file name are different! i've been there