一个同时支持 Objective-C、Swift 和 SwiftUI 的 iOS 主题框架。
- 统一的主题管理器,支持跟随系统或手动选择浅色/深色。
- UIKit 动态颜色封装(
UIColor便捷属性与ThemeDynamicColor)。 - SwiftUI 桥接(
Color便捷属性与ThemeObserver)。 - 可扩展的颜色 Token 与调色板覆盖机制。
- Demo 工程演示 UIKit + SwiftUI 的组合使用。
- iOS 13+
- Swift tools 5.9(Xcode 16+)
- File > Add Package Dependencies...
- 输入地址:
https://github.com/FollowmeTech/ThemeKit.git - 将
ThemeKit添加到目标工程。
.package(url: "https://github.com/FollowmeTech/ThemeKit.git", from: "1.0.0")然后添加产品依赖:
.product(name: "ThemeKit", package: "ThemeKit")在应用启动时配置主题(SceneDelegate 或 AppDelegate):
import ThemeKit
ThemeManager.shared.updateSystemInterfaceStyleIfNeeded(windowScene.traitCollection)
ThemeManager.shared.configure(with: ThemeCatalog.default)
ThemeManager.shared.register(window: window)使用主题颜色:
view.backgroundColor = .themeBackground
label.textColor = .themePrimaryText
button.tintColor = .themeAccent切换主题:
ThemeManager.shared.selectTheme(.dark) // .light / .followSystem使用 ThemeObserver 在主题切换时触发视图刷新:
import ThemeKit
@StateObject private var themeObserver = ThemeObserver()
var body: some View {
VStack(spacing: 12) {
Text("ThemeKit")
.foregroundColor(.themePrimaryText)
Text(themeObserver.selection == .dark ? "Dark" : "Light")
.foregroundColor(.themeSecondaryText)
}
.padding()
.background(Color.themeBackground)
}@import ThemeKit;
[[ThemeManager shared] selectTheme:ThemeSelectionDark];
self.view.backgroundColor = [UIColor themeBackground];
self.label.textColor = [UIColor themePrimaryText];扩展 ThemeColorToken,覆盖调色板并重新配置:
extension ThemeColorToken {
static let demoCard = ThemeColorToken("demo.card")
}
let base = ThemeCatalog.default
let lightPalette = base.lightTheme.palette.applyingOverrides([
.demoCard: UIColor(red: 0.85, green: 0.96, blue: 0.90, alpha: 1.0),
.accent: .systemPink
])
let darkPalette = base.darkTheme.palette.applyingOverrides([
.demoCard: UIColor(red: 0.26, green: 0.08, blue: 0.14, alpha: 1.0),
.accent: .systemOrange
])
let catalog = ThemeCatalog(
lightTheme: Theme(interfaceStyle: .light, palette: lightPalette),
darkTheme: Theme(interfaceStyle: .dark, palette: darkPalette)
)
ThemeManager.shared.configure(with: catalog)打开示例工程并运行 ThemeDemo:
open Demo/ThemeDemo.xcodeprojswift testMIT。详见 LICENSE。