NSFontAttributeName has changed to String

前端 未结 3 1604
走了就别回头了
走了就别回头了 2020-12-13 13:05

i\'m trying to style the navigation bar properly, i need to change the font to helvetica neue with a size point of 19. I\'ve ever used this code but i\'ve notice that now do

3条回答
  •  感动是毒
    2020-12-13 13:54

    The UIFont constructor is returning an optional (UIFont?) which you must unwrap to use. Add ! if you're sure you have a valid font name:

    Swift 4.2:

    navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.font: UIFont(name: "HelveticaNeue-Light", size: 19)!]
    

    Swift 4:

    navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Light", size: 19)!]
    

    Swift 3:

    navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "HelveticaNeue-Light", size: 19)!]
    

    Note: If you are setting a font with a static name in your code, then force unwrapping is safe once you’ve verified you’re using a valid font name. If you are getting the font name from an external source (the user or a server), you will want to use optional binding such as if let font = UIFont(... or guard let font = UIFont(... to safely unwrap the font before use.

提交回复
热议问题