跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • 一个 Swift 编写,轻量简洁的 iOS 主题管理库
未分類
14 1 月 2021

一个 Swift 编写,轻量简洁的 iOS 主题管理库

一个 Swift 编写,轻量简洁的 iOS 主题管理库

資深大佬 : Yunhao 11

苹果在 iOS 13 推出了 Dark Mode,可以让 iOS App 拥有 Light 和 Dark 两套主题。但是,如果你的 App 需要支持更多主题切换,可以试一试 Crystal,一个轻量、简洁的 iOS 主题管理库。

https://github.com/yunhao/Crystal

如果你对这个库感兴趣,欢迎 Star 。

定义主题,类型安全

你可以使用 class,struct 甚至 enum 来定义你的 Theme,然后可以在项目的某个地方集中管理这些主题:

// 定义主题类型。 public struct AppTheme {     var textColor: UIColor     var backgroundColor: UIColor }  // 实例化主题,一个主题就是一个对象。 extension AppTheme {     static var light: AppTheme {         return AppTheme(textColor: .black, backgroundColor: .white)     }      static var sunny: AppTheme {         return AppTheme(textColor: .yellow, backgroundColor: .white)     }     // ... 更多主题 }  extension AppTheme: CrystalThemeType {     // 返回 app 启动时的初始主题。     public static var entry: CrystalThemeType { Self.light } } 

主题不需要 Hard-coding 。结合 Swift Codable 协议,你还可以读取本地文件、从网络请求 JSON,然后动态加载主题。

设置主题,简洁

所有由库引入的扩展方法都暴露在目标对象的 .cst 属性下,无其他额外属性引入,不会污染目标对象。

你只需要把主题的值赋予目标对象上,就像在 UIKit 中经常做的那样。即使之后增加、删除主题,这部分代码也不需要修改!

// 为按钮设置主题。 doneButton.cst.apply { button, theme in     button.setTitleColor(theme.textColor, for: .normal) } // 自定义 view 。 cardView.cst.apply { card, theme in      card.backgroundColor = theme.backgroundColor     card.someTextColor = theme.textColor } // Swift 简写。 imageView.cst.apply { $0.tintColor = $1.textColor } 

切换主题

// 切换主题 Crystal.shared.theme = .sunny  // 或者,加一个动画 UIView.animate {   Crystal.shared.theme = .light } 

修改主题,轻松维护

// 添加一个新的主题 extension AppTheme {     static var summer: AppTheme {         return AppTheme(textColor: .red, backgroundColor: .white)     } }  Crystal.shared.theme = .summer  // 你可以直接设置一个新的主题。 Crystal.shared.theme = AppTheme(textColor: .red, backgroundColor: .white) 

为了不过于冗长,这里省略了部分代码,欢迎到项目主页查看 README 。

Crystal 使用 Weak Key Dictionary 来管理这些对象和闭包,在必要时自动释放以防止内存泄漏,具体实现可参考源码。

项目地址: https://github.com/yunhao/Crystal

大佬有話說 (2)

  • 資深大佬 : vincentxue

    代码写的不错,如果是非侵入式设计就好了。

  • 主 資深大佬 : Yunhao

    @vincentxue 感谢。继续深入学习 Swift,看能不能写的再好一些~

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具