web-dev-qa-db-fra.com

Comment changer la police de navigation de la barre de navigation dans Swift?

Comment changer la police NavigationBar dans Swift?

C'est ce que j'ai essayé jusqu'à présent, mais j'ai reçu une erreur (j'ai correctement implémenté CaviarDreams dans le projet):

self.navigationController.navigationBar.titleTextAttributes = NSFontAttributeName[UIFont .fontWithName(CaviarDreams.ttf, size: 20)]

Erreur dit: Use of unresolved identifier 'CaviarDreams

Désolé si la question est extrêmement mauvaise.

33
b3rge

Essaye ça:

self.navigationController.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "CaviarDreams", size: 20)!]

Edit: Maintenant, UIFont doit être décompressé pour pouvoir être utilisé ici.

95
Jim T

En utilisant Swift, j’ai ajouté ceci à AppDelegate.Swift dans 

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.

        UINavigationBar.appearance().titleTextAttributes = [
            NSFontAttributeName: UIFont(name: "DINNextLTW04-Regular", size: 20)!
        ]

        return true
    }

J'espère que ça aide!

42
Tim S

Swift 2.0:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        UINavigationBar.appearance().titleTextAttributes = [
            NSFontAttributeName: UIFont(name: "Arial-Regular", size: 30)!
        ]

        return true
    }

Ou

 override func viewDidLoad() {
  super.viewDidLoad()

  self.navigationController?.navigationBarHidden =  false
  self.title = "SAMPLE"

//Set Color
  let attributes: AnyObject = [ NSForegroundColorAttributeName: UIColor.redColor()]
  self.navigationController!.navigationBar.titleTextAttributes = attributes as? [String : AnyObject]


//Set Font Size
  self.navigationController!.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Arial", size: 37.0)!];

 }
12
A.G

Maintenant, vous devez d'abord le décompresser (!) Pour qu'il ne soit pas de type UIFont?:

self.navigationController.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "<font-name>", size: <size>)!]
10
benaneesh

Pour Swift 2.3

self.navigationController!.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Roboto-Bold", size: 20.0)!, NSForegroundColorAttributeName : UIColor.whiteColor()];
4
Amr Angry

Swift 4

if let font = UIFont(name: "FontName", size: 16) {

 self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: font]

}

Ou comme l’autre réponse l’a recommandé sur AppDelegate:

  if let font = UIFont(name: "FontName", size: 16) {

    UINavigationBar.appearance().titleTextAttributes = [
          NSAttributedStringKey.font: font]

}
3
Sam Bing

Swift 4.2

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  // Override point for customization after application launch.

  let font = UIFont(name: "FontName", size: 16) ?? UIFont.systemFont(ofSize: 16)
  UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.font: font]
}
0
xmasalov

Swift 4.2

 self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.font: UIFont(name: "Helvetica", size: 18.0)!]
0
Nish