J'ai essayé de changer la police des éléments de la barre d'onglets mais je n'ai pas pu trouver d'exemples Swift. Je sais que c'est ainsi que vous les changez dans Objective-C:
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"AmericanTypewriter" size:20.0f], UITextAttributeFont, nil] forState:UIControlStateNormal];
Mais comment puis-je traduire cela en Swift?
UITextAttributeFont a été déprécié dans iOS 7. Vous devez utiliser la variante NS à la place:
import UIKit
let appearance = UITabBarItem.appearance()
let attributes = [NSFontAttributeName:UIFont(name: "American Typewriter", size: 20)]
appearance.setTitleTextAttributes(attributes, forState: .Normal)
Swift 4.2
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "FontName", size: 10)!], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "FontName", size: 10)!], for: .selected)
Swift 4
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "FontName", size: 10)!], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "FontName", size: 10)!], for: .selected)
Swift
UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Font-Name", size: 10)!], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Font-Name", size: 10)!], for: .selected)
Remarque: Utilisez setTitleTextAttributes
pour les deux .normal
et .selected
pour que les modifications conservent les modifications de l'état de sélection.
Mettez ceci sous didFinishLaunchingWithOptions
:
UITabBarItem.appearance()
.setTitleTextAttributes(
[NSAttributedStringKey.font: UIFont(name: "Didot", size: 10)!],
for: .normal)
Cela fonctionne dans Swift 4
En plus de la réponse de @ Mc.Lover, si vous souhaitez appliquer cette modification à tous vos éléments de barre d'onglets dans l'application, je recommande d'ajouter le code dans la fonction application
de la classe AppDelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//Just add this line to get it done.
UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "IranSansMobile", size: 15)!], for: UIControlState.normal)
return true
}
Dans la version Swift4, vous pouvez utiliser des clés d'attribut pour définir la police et la couleur de premier plan
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor(hexString: "#D8FFE8"), NSAttributedStringKey.font : UIFont(name: "HelveticaNeue-Bold", size: 16) as Any], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor(hexString: "#FFFFFF"),NSAttributedStringKey.font : UIFont(name: "HelveticaNeue-Bold", size: 16) as Any], for: .selected)
Swift 4.2
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "BahijTheSansArabic-Plain", size: 10)!], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "BahijTheSansArabic-Plain", size: 10)!], for: .selected)