【问题标题】:How to set UIButton font via appearance proxy in iOS 8?如何在 iOS 8 中通过外观代理设置 UIButton 字体?
【发布时间】:2015-07-03 22:12:01
【问题描述】:

我尝试通过外观代理设置UIButton 的字体。但这似乎不起作用。这是我尝试过的。

UIButton.appearance().titleFont = UIFont(name: FONT_NAME_DEFAULT, size:20.0) UIButton.appearance().titleLabel?.font = UIFont(name: FONT_NAME_DEFAULT, size:20.0)

如何在 iOS 8 中通过外观代理设置UIButton 字体?

编辑:在 vaberer 的 link 中找到:“我很惊讶 UIButton 没有任何 UI_APPEARANCE_SELECTOR 属性,但符合 UIAppearance 协议。”

【问题讨论】:

  • 从 iOS 9 开始,您可以使用 UILabel.appearance(whenContainedInInstancesOf: [UIButton.self]).font = ...

标签: ios swift uibutton uiappearance


【解决方案1】:

在支持主题的应用程序中遇到了同样的问题。

1。添加此扩展程序

// UIButton+TitleLabelFont.swift

import UIKit

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

2。然后设置UIButton外观原型对象

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。在应用委托中删除主题设置

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    Theme.apply()

    // ...

    return true
}

如果您提前预加载内容(来自故事板的lazy var VC),最好不要使用应用委托设置主题内容,而是像这样在覆盖初始化程序中设置主题内容:

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()
        }
    }
}

【讨论】:

  • 第 1 步是个好主意,我只需要以标准方式使用外观 API。
  • 不确定这是否可行,因为 Apple 似乎从 UILabel 的外观 API 中删除了该字体。另外,我相信您可以只使用 UIButton 中标签的“包含”版本,而不必使用扩展名,但同样,现在也行不通。
【解决方案2】:

Barry 的回答效果很好,但 UIButton 扩展实现已过时 - 您需要声明 var titleLabelFont 喜欢:

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

在此处查看完整实现:​​http://blog.iseinc.biz/use-uiappearance-create-ios-app-themes

【讨论】:

  • 我尝试了 Barry 的解决方案(没有 NSObject 扩展名),但没有成功。添加@objc dynamic 是让它发挥作用的关键。
【解决方案3】:

使用 UIAppearance 设置 UIButton 的字体如下:

label = UILabel.appearance(whenContainedInInstancesOf: [UIButton.self])
label.font = UIFont.systemFont(ofSize: 20)

您可以在 AppDelegate 的方法 application(_:, didFinishLaunchingWithOptions:) 中添加上述代码,或者在适合为您的应用程序设置主题的任何地方添加。

UIViewController.viewDidLoad 中,将按钮实例的titleLabeladjustsFontForContentSizeCategory 属性设置为true。

class ExampleVC: UIViewController

    @IBOutlet weak var btnOkay: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        btnOkay.titleLabel?.adjustsFontForContentSizeCategory = true
    }
}

【讨论】:

    【解决方案4】:

    您正在尝试在UIButton 内设置UILabel 的字体。因为UILabel 没有用 UI_APPEARANCE_SELECTOR 标记。你不能这样。

    查看 UIAppearance 元素列表:What properties can I set via an UIAppearance proxy?

    【讨论】:

    • 在您的链接中找到:“我很惊讶 UIButton 没有任何 UI_APPEARANCE_SELECTOR 属性,但符合 UIAppearance 协议。这是 Apple 的疏忽吗?”
    • 是的,但是由于titleLabel属性返回可选的UILabel,你需要看一下UILabel的外观。
    【解决方案5】:

    当包含在按钮中时,您可以将其应用于标签:

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

    【讨论】:

    • 这可能听起来很奇怪,但是在故事板中将字体大小设置为 17,它应该会启动。似乎是一个神奇的默认数字或错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多