2014-09-26 10 views
16

Ho cercato di modificare il carattere per gli elementi della barra delle schede, ma non sono stato in grado di trovare alcun esempio Swift. So che questo è come la si modifica in Objective-C:Modifica del carattere della barra delle schede in Swift

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

Ma come posso tradurre questo in Swift?

risposta

37

L'UITextAttributeFont è stata sconsigliata a iOS 7. Si consiglia di utilizzare la variante NS invece:

import UIKit 

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

Grazie per la risposta, ma non sembra cambiare nulla ... – user3746428

+0

Nevermind, ho messo il codice in awakeFromNib e quello lo ha riparato. Molte grazie! – user3746428

+0

Nota: l'utilizzo di Playground è un ottimo modo per scoprire questo genere di cose. Puoi anche dare un'occhiata a http://swifter.natecook.com per scoprire alcuni dei nomi. – AlBlue

15

Ecco Swift 3.0 soluzione

UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Font-Name", size: 10)!], for: .normal) 
UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Font-Name", size: 10)!], for: .selected) 
+3

Questo non cambia il font per me per lo stato '.selected'. – AbbasAngouti

+0

@AbbasAngouti - devi solo duplicare il codice e cambiare' .normal' in '.selected' –

2

Inoltre @ risposta Mc.Lover s', Se vuole aplly questo cambiamento a tutti i tuoi tab Bar I prodotti in applicazione, Consiglierei ad aggiungere il codice in application funzione di 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 
} 
3

mettere questo sotto didFinishLaunchingWithOptions:

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

Questo funziona in Swift 4