Unable to set navigation bar font in Swift on latest version of Xcode

The following code worked fine until upgrading to Xcode 6.1 to stay with iOS 8.1:

override func viewDidAppear(animated: Bool) { self.navigationController?.navigationBar.topItem?.title = "Home" self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 34), NSForegroundColorAttributeName: UIColor.whiteColor()] } 

The problem is specifically in NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 34)

and the error I get here:

! "Could not find an overload for 'init' that accepts the supplied arguments"

I found this original code in another StackOverflow question, and it worked as expected before this update (downloaded it yesterday). My font is really installed correctly.

Should I write this code differently now, or is there a whole new way that I have to set my Navigation Bar Font?

Thanks!

+6
source share
6 answers

Oops I figured it out on my own:

I needed an exclamation point after my NSFontAttributeName declaration, because it requires the type "NSString!". Perhaps this only required an "NSString", but I have no problem right now.

Working line:

 NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 24)! 

Working full code:

 override func viewDidAppear(animated: Bool) { self.navigationController?.navigationBar.topItem?.title = "Home" self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 34)!, NSForegroundColorAttributeName: UIColor.whiteColor()] } 

Now it seems like a silly question. Hope this helps someone else!

+21
source

You are using UIFont(name: intializer, as it is defined as

init?(name fontName: String, size fontSize: CGFloat) -> UIFont

failable intializer read more from link.So it returns optional.You need to deploy it because it requires AnyObject is optional.

  self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 34)!, NSForegroundColorAttributeName: UIColor.whiteColor()] 
+2
source

Thanks Benji !!!

I changed it a bit and applied it to the appearance attribute of the navigation controller.

  var navigationBarAppearance = UINavigationBar.appearance() navigationBarAppearance.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "OpenSans", size: 16)!, NSForegroundColorAttributeName: UIColor.whiteColor()] 
+2
source

It is better to declare your font as conditional, for example as follows:

  if let font = UIFont (name: "AdobeArabic-BoldItalic", size: 20) { UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: font] } 

When doing this, make sure that your font is already found, that I installed a new font, and when it is used without a conditional, if it throws an exception, the deployment is optional.

+2
source
 override func viewWillLayoutSubviews() { self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName : UIFont(name: "Roboto-Regular", size: 14)!] } 
0
source

Swift 4.2

Change the small name when the layout scrolls down

 override func viewDidAppear(animated: Bool) { self.navigationController?.navigationBar.topItem?.title = "Home" self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 34), NSForegroundColorAttributeName: UIColor.whiteColor()] } 

Change Large Title

 override func viewDidAppear(animated: Bool) { self.navigationController?.navigationBar.topItem?.title = "Home" self.navigationController?.navigationBar.largeTitleTextAttributes = [ NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 34), NSForegroundColorAttributeName: UIColor.whiteColor()] } 
0
source

Source: https://habr.com/ru/post/1205476/


All Articles