SwiftUI 的主题系统。
该库旨在尽可能简化主题颜色的访问。配置完成后,你就可以像使用常规 SwiftUI 颜色一样使用主题颜色,例如使用 foregroundColor
、fill
等修饰符。以下代码列出了你可以使用此库执行的一些操作的示例。
import SwiftUI
import SwiftUITheme
struct ContentView: View {
// Read current theme from environment
@Environment(\.theme) private var theme
var body: some View {
// Access any value from theme
Text("Current theme is \(theme.name)")
// Use colors from theme as if they were regular colors (no need to read theme from environment)
Text("Hello").foregroundColor(.colorFromTheme)
VStack {
Rectangle().fill(.otherColorFromTheme)
}
.theme(.differentTheme) // Override theme for specific view hierarchy
}
}
以下代码片段列出了配置该库所需的最小代码量。你可以复制并粘贴到你的项目中,并填写必要的细节。
有关所有详细信息和其他定义主题的方法,请参阅文档。
import SwiftUI
import SwiftUITheme
struct Theme {
let <#color from theme#>: Color
}
extension Theme: BaseTheme {
static let defaultValue: Theme = <#default theme#>
static let environmentValue: WritableKeyPath<EnvironmentValues, Theme> \.theme
}
extension EnvironmentValues {
var theme: Theme {
get { self[Theme.self] }
set { self[Theme.self] = newValue }
}
}
extension ThemeColor where Self == ThemeColor<Theme> {
static var <#color from theme#>: Self { Self(\.<#color from theme#>) }
}
SwiftUITheme 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。