主题化

Build Status

在 Swift 中轻松实现类型安全的用户界面主题化。

这个项目还处于早期阶段,欢迎所有反馈。对如何改进这个库有任何建议吗?请留下 issue :)

非常欢迎 Pull Request。

特性

安装

使用 CocoaPods 安装

# In your Podfile add the following, then
# save and run `pod install`:
pod 'Themeable'

使用示例

import UIKit
import Themeable

// Define the theme and its properties to be used throughout your app
struct MyAppTheme: Theme {

    let identifier: String
    let seperatorColor: UIColor
    let lightBackgroundColor: UIColor
    let statusBarStyle: UIStatusBarStyle

    static let light = MyAppTheme(
        identifier: "co.brushedtype.Themeable.light-theme",
        seperatorColor: .lightGray,
        lightBackgroundColor: .white,
        statusBarStyle: .default
    )

    static let dark = MyAppTheme(
        identifier: "co.brushedtype.Themeable.dark-theme",
        seperatorColor: .black,
        lightBackgroundColor: .gray,
        statusBarStyle: .lightContent
    )

    // Expose the available theme variants
    static let variants: [MyAppTheme] = [ .light, .dark ]
    
    // Expose the shared theme manager
    static let manager = ThemeManager<MyAppTheme>(default: .light)

}

// Conform to the `Themeable` protocol and register for updates
class TableViewController: UITableViewController, Themeable {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // register the themeable items once all the view and subviews
        // have been loaded
        MyAppTheme.manager.register(themeable: self)
    }

    // function will be called whenever the theme changes
    func apply(theme: MyAppTheme) {
        self.tableView.separatorColor = theme.seperatorColor
        self.tableView.backgroundColor = theme.lightBackgroundColor
    }

}

许可

MIT 许可证 (MIT)