swift - How to set UIButton font via appearance proxy in iOS 8? -


i tried set font of uibutton via appearance proxy. doesn't seem work. tried.

uibutton.appearance().titlefont = uifont(name: font_name_default, size:20.0) uibutton.appearance().titlelabel?.font = uifont(name: font_name_default, size:20.0)

how set uibutton font via appearance proxy in ios 8 ?

edit: found in vaberer's link: "i'm surprised uibutton doesn't have ui_appearance_selector properties, yet conforms uiappearance protocol."

had same problem theme-able app.

1. add extension

// uibutton+titlelabelfont.swift  import uikit  extension uibutton {     var titlelabelfont: uifont! {         { return self.titlelabel?.font }         set { self.titlelabel?.font = newvalue }     } } 

2. setup uibutton appearance prototype object

class theme {     static func apply() {        applytouibutton()        // ...     }      // can either theme specific uibutton instance, or defaults appearance proxy (prototype object) default     static func applytouibutton(a: uibutton = uibutton.appearance()) {        a.titlelabelfont = uifont(name: font_name_default, size:20.0)        // other uibutton customizations     } } 

3. drop theme setup in app delegate

func application(application: uiapplication, didfinishlaunchingwithoptions launchoptions: [nsobject: anyobject]?) -> bool {     theme.apply()      // ...      return true } 

if you're preloading stuff (lazy var vcs storyboards) earlier, may better instead of using app delegate setup theme stuff in override initializer so:

private var _nsobject__theme_apply_token: dispatch_once_t = 0  extension nsobject {     override public class func initialize() {         super.initialize()         // see also: https://stackoverflow.com/questions/19176219/why-am-i-getting-deadlock-with-dispatch-once         var shouldrun = false         dispatch_once(&_nsobject__theme_apply_token) {             shouldrun = true         }         if shouldrun {             theme.apply()         }     } } 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -