晶石 1.1.0

Crystal 1.1.0

rhetttuan 维护。




Crystal 1.1.0

  • Rhett Tuan

Crystal

SPM support Carthage
GitHub license Cocoapods platforms


晶石是一个轻量级且直观的 iOS 主题管理器。它利用 Swift 的功能提供了一个易于使用的界面。使用晶石,您可以有信心地将主题集成到您的应用中,同时具有灵活性。

Crystal

特性

  • 灵活:晶石与任何对象兼容,而不仅仅是内置 UI 组件。您可以在任何地方使用晶石。
  • 友好:以您熟悉的方式应用主题,无需额外的属性 API,让您感觉困惑和分心。
  • 简单:添加主题就像创建一个实例一样简单。使用晶石轻松维护您的主题。
  • 类型安全:充分利用 Swift 的类型安全。有信心地应用主题,并从编译时检查中受益。

入门

用法

使用晶石有三个步骤: 定义主题类型 -> 使兼容 -> 应用主题

步骤 1 - 定义主题类型

您可以通过使 classstruct 符合 CrystalThemeType 协议来介绍您的主题类型。使用此协议,您必须实现一个静态属性来返回入口主题。

public struct AppTheme {
    var textColor: UIColor
    var backgroundColor: UIColor
}

extension AppTheme: CrystalThemeType {
    static var light: AppTheme {
        return AppTheme(
            textColor: .black,
            backgroundColor: .white
        )
    }

    static var dark: AppTheme {
        return AppTheme(
            textColor: .white,
            backgroundColor: .black
        )
    }

    // Return the entry theme.
    public static var entry: CrystalThemeType {
        return Self.light
    }
}

步骤 2 - 使其兼容

为了告诉 Crystal 关于您的主题类型

  • Crystal 符合 CrystalDetermined
  • 根据您的需求,使任何类型符合 CrystalDetermined

使用 typealias 来确定具体的主题类型。

// Required. Determine the concrete theme type.
extension Crystal: CrystalDetermined {
    public typealias Theme = AppTheme
}

// Optional. Make any type compatible as you need.
extension UIView: CrystalCompatible {
    public typealias Theme = AppTheme
}
extension UIBarButtonItem: CrystalCompatible {
    public typealias Theme = AppTheme
}

任何符合 CrystalCompatible 协议的对象都有一个 cst 命名空间来公开 Crystal 方法。

步骤 3 - 应用主题

使用 apply(_:) 方法来应用主题,您可以在闭包中执行任何想要的操作

// Apply theme to a button.
doneButton.cst.apply { button, theme in
    button.setTitleColor(theme.textColor, for: .normal)
}
// Apply theme to a custom view.
cardView.cst.apply { card, theme in 
    card.backgroundColor = theme.backgroundColor
    card.titleColor = theme.textColor
}
// Shorthand argument names.
imageView.cst.apply { $0.tintColor = $1.textColor }

赋予 Crystal.shared.theme 新值以更改主题

// Change theme.
Crystal.shared.theme = .dark

需求

  • Swift 5.0+
  • iOS 10.0+

安装

CocoaPods

当使用CocoaPods时,请在您的Podfile中添加以下内容

pod 'Crystal'

Carthage

对于Carthage,请在您的Cartfile中添加以下内容

github "yunhao/Crystal"

Swift 包管理器

将以下内容添加到您的Package.swift文件的依赖关系部分

.package(url: "https://github.com/yunhao/Crystal.git", from: "1.0.0")

贡献

欢迎提交拉取请求、错误报告和功能请求。

许可证

Crystal 在< 色="https://github.com/yunhao/Crystal-Test/blob/master/LICENSE"">MIT 许可证下发布。