Colors
Colors is a Swift Package to enable all system colors in SwiftUI trough a Color extension. Colors which were previously only available in UIColor/NSColor are now available in Color as well.
Install / Use
/learn @0xWDG/ColorsREADME
Colors
Colors is a Swift Package to enable all system colors in SwiftUI trough a Color extension.
Colors which were previously only available in UIColor/NSColor are now available in Color as well.
Available colors are: 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.
Requirements
- Swift 5.9+ (Xcode 15+)
- iOS 13+, macOS 10.15+
Installation (Pakage.swift)
dependencies: [
.package(url: "https://github.com/0xWDG/Colors.git", branch: "main"),
],
targets: [
.target(name: "MyTarget", dependencies: [
.product(name: "Colors", package: "Colors"),
]),
]
Installation (Xcode)
- In Xcode, open your project and navigate to File → Swift Packages → Add Package Dependency...
- Paste the repository URL (
https://github.com/0xWDG/Colors) and click Next. - Click Finish.
Usage
import SwiftUI
import Colors
struct ContentView: View {
var body: some View {
VStack {
Text("Hello, World!")
.foregroundColor(Color.disabledControlTextColor)
}
.padding()
}
}
Extract color from UIColor/NSColor
Use this to add new/missing colors to the BaseColor and Color extension.
Extract from UIKit:
UIColor.systemPink.createInitializerFor(color: "systemPink")
Extract from AppKit:
NSColor.systemPink.createInitializerFor(color: "systemPink")
Output:
/// 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)
)
How to add a new color
- Add the color to the
BaseColorstruct./// 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) ) - Add the color to the
Colorextension.- Use the native
Color,NSColor,UIColor.colorNamewhere possible. - Add #if os(iOS) / #if os(macOS) where needed.
- Example (works on almost all versions):
/// 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 }- Example 2 (works from a specific iOS/macOS version):
/// 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 } - Use the native
Contact
🦋 @0xWDG 🐘 mastodon.social/@0xWDG 🐦 @0xWDG 🧵 @0xWDG 🌐 wesleydegroot.nl 🤖 Discord
Interested learning more about Swift? Check out my blog.
