Próbowałem zmienić czcionkę dla elementów paska kart, ale nie udało mi się znaleźć żadnych przykładów Swift. Wiem, że tak to zmieniasz w Objective-C:
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"AmericanTypewriter" size:20.0f], UITextAttributeFont, nil] forState:UIControlStateNormal];
Ale jak mogę to przełożyć na Swift?
Odpowiedzi:
42 dla odpowiedzi № 1UITextAttributeFont został uznany za przestarzały w iOS 7. Zamiast tego należy użyć wariantu NS:
import UIKit
let appearance = UITabBarItem.appearance()
let attributes = [NSFontAttributeName:UIFont(name: "American Typewriter", size: 20)]
appearance.setTitleTextAttributes(attributes, forState: .Normal)
23 dla odpowiedzi nr 2
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 3
UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Font-Name", size: 10)!], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Font-Name", size: 10)!], for: .selected)
Uwaga: Posługiwać się setTitleTextAttributes
dla obu .normal
i .selected
aby zmiany zachowywały zmiany stanu selekcji.
5 dla odpowiedzi nr 3
Połóż to pod didFinishLaunchingWithOptions
:
UITabBarItem.appearance()
.setTitleTextAttributes(
[NSAttributedStringKey.font: UIFont(name: "Didot", size: 10)!],
for: .normal)
Działa to w Swift 4
3 dla odpowiedzi № 4
Oprócz odpowiedzi @ Mc.Lover, jeśli chcesz zastosować tę zmianę do wszystkich elementów paska kart w aplikacji, polecam dodać kod w application
funkcja klasy 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
}
0 dla odpowiedzi № 5
W wersji Swift4 możesz użyć klawiszy atrybutów, aby ustawić czcionkę i kolor pierwszego planu
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)