web-dev-qa-db-fra.com

Modification de titleTextAttributes en rapide

Depuis que j'ai mis à jour xcode, je n'arrive pas à changer la titleTextAttribute. Maintenant, quand j'utilise le code suivant, j'obtiens cette erreur:

impossible de trouver un init de surcharge acceptant les arguments fournis

Code en appDelegate:

UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name: "Ubuntu", size: 17), NSForegroundColorAttributeName:UIColor.whiteColor()]
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Ubuntu-Light", size: 15), NSForegroundColorAttributeName:UIColor.whiteColor()], forState: UIControlState.Normal)
20
Peter Pik

Une modification récente a été apportée afin que UIFont(name:size:) renvoie une instance UIFont facultative. Vous devrez les déballer pour le faire fonctionner. Utiliser ! est le moyen le plus simple, mais vous obtiendrez un blocage si la police n’est pas sur le système. Essayez quelque chose comme:

let navbarFont = UIFont(name: "Ubuntu", size: 17) ?? UIFont.systemFontOfSize(17)
let barbuttonFont = UIFont(name: "Ubuntu-Light", size: 15) ?? UIFont.systemFontOfSize(15)

UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: navbarFont, NSForegroundColorAttributeName:UIColor.whiteColor()]
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: barbuttonFont, NSForegroundColorAttributeName:UIColor.whiteColor()], forState: UIControlState.Normal)
37
Nate Cook

Swift 4: 

UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.white], for: .normal)
UINavigationBar.appearance().titleTextAttributes = [
            NSAttributedStringKey.foregroundColor: UIColor.white
        ]
7
Steve Moser

Pour Swift 3, vous pouvez essayer les solutions suivantes:

UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
2
Andrey Kirsanov

Swift 4: 

if let navFont = UIFont(name: "Futura", size: 18) {
   let navBarAttributesDictionary: [NSAttributedStringKey: Any] = [
   NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue): UIColor(netHex: Colors.BabyBlue.rawValue),
   NSAttributedStringKey(rawValue: NSAttributedStringKey.font.rawValue): navFont ]
   UINavigationBar.appearance().titleTextAttributes = navBarAttributesDictionary
}
0
Satyam
    if let navFont = UIFont(name: "HelveticaNeue-Bold", size: 30.0) {
        let navBarAttributesDictionary: [NSObject: AnyObject]? = [
            NSForegroundColorAttributeName: UIColor.blackColor(),
            NSFontAttributeName: navFont
        ]
        navigationController?.navigationBar.titleTextAttributes = navBarAttributesDictionary
    }
0
Durul Dalkanat