/ / Ändern der Tab-Leisten-Schriftart in Swift - Swift, Uitabbar, Uifont

Ändern der Schriftart der Registerkarten in Swift - swift, uitabbar, uifont

Ich habe versucht, die Schriftart für die Elemente der Registerkartenleiste zu ändern, konnte jedoch keine Swift-Beispiele finden. Ich weiß, dass Sie diese in Objective-C folgendermaßen ändern:

[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"AmericanTypewriter" size:20.0f], UITextAttributeFont, nil] forState:UIControlStateNormal];

Aber wie kann ich das in Swift übersetzen?

Antworten:

42 für die Antwort № 1

Das UITextAttributeFont war in iOS 7 veraltet. Verwenden Sie stattdessen die NS-Variante:

import UIKit

let appearance = UITabBarItem.appearance()
let attributes = [NSFontAttributeName:UIFont(name: "American Typewriter", size: 20)]
appearance.setTitleTextAttributes(attributes, forState: .Normal)

23 für die Antwort № 2

Schnell 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)

Schnell 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)

Hinweis: Benutzen setTitleTextAttributes für beide .normal und .selected Änderungen beibehalten Änderungen des Auswahlstatus.


5 für die Antwort № 3

Setze das unter didFinishLaunchingWithOptions:

UITabBarItem.appearance()
.setTitleTextAttributes(
[NSAttributedStringKey.font: UIFont(name: "Didot", size: 10)!],
for: .normal)

Dies funktioniert in Swift 4


3 für die Antwort № 4

Wenn Sie diese Änderung auf alle Ihre Registerkartenelemente in der Anwendung anwenden möchten, empfehle ich, den Code hinzuzufügen application Funktion der AppDelegate-Klasse:

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 für die Antwort № 5

In der Swift4-Version können Sie Attributtasten verwenden, um die Schriftart und die Vordergrundfarbe festzulegen

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)