Colors 是一个 Swift 包,通过 Color
扩展在 SwiftUI 中启用所有系统颜色。
以前仅在 UIColor
/NSColor
中可用的颜色现在也可以在 Color
中使用。
可用的颜色有:lightText
、darkText
、placeholderText
、label
、secondaryLabel
、tertiaryLabel
、quaternaryLabel
、systemBackground
、secondarySystemBackground
、tertiarySystemBackground
、systemFill
、secondarySystemFill
、tertiarySystemFill
、quaternarySystemFill
、systemGroupedBackground
、secondarySystemGroupedBackground
、tertiarySystemGroupedBackground
、systemGray
、systemGray2
、systemGray3
、systemGray4
、systemGray5
、systemGray6
、separator
、opaqueSeparator
、link
、systemBlue
、systemCyan
、systemMint
、systemPurple
、systemGreen
、systemYellow
、systemOrange
、systemPink
、systemRed
、systemTeal
、systemIndigo
、scrubberTexturedBackground
、textBackgroundColor
、controlTextColor
、quaternaryLabelColor
、findHighlightColor
、highlightColor
、shadowColor
、windowFrameTextColor
、windowBackgroundColor
、keyboardFocusIndicatorColor
、separatorColor
、selectedControlColor
、controlBackgroundColor
、secondaryLabelColor
、tertiaryLabelColor
、gridColor
、alternateSelectedControlTextColor
、unemphasizedSelectedContentBackgroundColor
、textColor
、systemBrown
、selectedContentBackgroundColor
、selectedTextColor
、labelColor
、placeholderTextColor
、unemphasizedSelectedTextBackgroundColor
、disabledControlTextColor
、headerTextColor
、linkColor
、selectedTextBackgroundColor
、unemphasizedSelectedTextColor
、controlColor
、selectedControlTextColor
、underPageBackgroundColor
、selectedMenuItemTextColor
。
dependencies: [
.package(url: "https://github.com/0xWDG/Colors.git", branch: "main"),
],
targets: [
.target(name: "MyTarget", dependencies: [
.product(name: "Colors", package: "Colors"),
]),
]
https://github.com/0xWDG/Colors
) 并点击 Next。import SwiftUI
import Colors
struct ContentView: View {
var body: some View {
VStack {
Text("Hello, World!")
.foregroundColor(Color.disabledControlTextColor)
}
.padding()
}
}
使用此方法向 BaseColor
和 Color
扩展添加新的/缺失的颜色。
从 UIKit 提取
UIColor.systemPink.createInitializerFor(color: "systemPink")
从 AppKit 提取
NSColor.systemPink.createInitializerFor(color: "systemPink")
输出
/// A color that represents the system-provided systemPink color.
public static let systemPink = Color.dynamicColor(
light: .init(red: 1.00, green: 0.18, blue: 0.33, alpha: 1.00),
dark: .init(red: 1.00, green: 0.18, blue: 0.33, alpha: 1.00)
)
BaseColor
结构体。/// A color that represents the system-provided systemPink color.
public static let systemPink = Color.dynamicColor(
light: .init(red: 1.00, green: 0.18, blue: 0.33, alpha: 1.00),
dark: .init(red: 1.00, green: 0.18, blue: 0.33, alpha: 1.00)
)
Color
扩展。Color
、NSColor
、UIColor.colorName
。 /// A color that represents the system-provided pink color.
public static var systemPink: Color {
#if os(iOS) || os(tvOS)
Color(UIColor.systemPink)
#elseif os(macOS)
Color(NSColor.systemPink)
#else
BaseColor.systemPink
#endif
}
/// A color that represents the system-provided cyan color.
public static var systemCyan: Color {
#if os(iOS) || os(tvOS)
if #available(iOS 15.0, *) {
Color(UIColor.systemCyan)
} else {
BaseColor.systemCyan
}
#elseif os(macOS)
if #available(macOS 12.0, *) {
Color(NSColor.systemCyan)
} else {
BaseColor.systemCyan
}
#else
BaseColor.systemCyan
#endif
}
🦋 @0xWDG 🐘 mastodon.social/@0xWDG 🐦 @0xWDG 🧵 @0xWDG 🌐 wesleydegroot.nl 🤖 Discord
有兴趣了解更多关于 Swift 的信息吗? 查看我的博客。