resumen libro latino fahrenheit español bradbury audiolibro analisis swift uitabbar uifont

swift - libro - fahrenheit 451 ray bradbury



Cambiar la fuente de la barra de pestañas en Swift (6)

He intentado cambiar la fuente de los elementos de la barra de pestañas, pero no he podido encontrar ningún ejemplo de Swift. Sé que así es como lo cambias en Objective-C:

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

Pero, ¿cómo puedo traducir esto en Swift?


Además, la respuesta de @ Mc.Lover, si desea aplicar este cambio a todos los elementos de la barra de pestañas en la aplicación, recomiendo agregar el código en la función de application de la clase 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 }


El UITextAttributeFont estaba en desuso en iOS 7. Debería usar la variante NS en su lugar:

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


En la versión Swift4, puede usar las teclas de atributo para establecer la fuente y el color de primer plano

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)


Ponga esto bajo didFinishLaunchingWithOptions :

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

Esto funciona en Swift 4.


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)


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)

Nota: use setTitleTextAttributes para .normal y .selected para que los cambios persistan en los cambios de estado de selección.