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

孤街浪徒 提交于 2019-11-29 12:27:59

Had the same problem with a theme-able app.

1. Add this extension

// UIButton+TitleLabelFont.swift

import UIKit

extension UIButton {
    var titleLabelFont: UIFont! {
        get { return self.titleLabel?.font }
        set { self.titleLabel?.font = newValue }
    }
}

2. Then setup the UIButton appearance prototype object

class Theme {
    static func apply() {
       applyToUIButton()
       // ...
    }

    // It can either theme a specific UIButton instance, or defaults to the appearance proxy (prototype object) by default
    static func applyToUIButton(a: UIButton = UIButton.appearance()) {
       a.titleLabelFont = UIFont(name: FONT_NAME_DEFAULT, size:20.0)
       // other UIButton customizations
    }
}

3. Drop the 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 from storyboards) earlier, it may be better to instead of using app delegate setup the theme stuff in an override initializer like 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()
        }
    }
}

Barry's answer works great but the UIButton extension implementation is out of date - you need declare var titleLabelFont like:

@objc dynamic var titleLabelFont: UIFont! {
    get { return self.titleLabel?.font }
    set { self.titleLabel?.font = newValue }
  }

See full implementation here: http://blog.iseinc.biz/use-uiappearance-create-ios-app-themes

You can apply it to the label when contained in a button:

UILabel.appearance(whenContainedInInstancesOf: [UIButton.self])
  .font = .systemFont(ofSize: 19, weight: .medium)
vaberer

You are trying to set a font of UILabel inside UIButton. Since UILabel is not tagged with UI_APPEARANCE_SELECTOR. You can't do it like this.

Look at the list of the UIAppearance elements at: What properties can I set via an UIAppearance proxy?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!