颜色

Colors 是一个 Swift 包,通过 Color 扩展在 SwiftUI 中启用所有系统颜色。
以前仅在 UIColor/NSColor 中可用的颜色现在也可以在 Color 中使用。

Swift Package Manager License

可用的颜色有:lightTextdarkTextplaceholderTextlabelsecondaryLabeltertiaryLabelquaternaryLabelsystemBackgroundsecondarySystemBackgroundtertiarySystemBackgroundsystemFillsecondarySystemFilltertiarySystemFillquaternarySystemFillsystemGroupedBackgroundsecondarySystemGroupedBackgroundtertiarySystemGroupedBackgroundsystemGraysystemGray2systemGray3systemGray4systemGray5systemGray6separatoropaqueSeparatorlinksystemBluesystemCyansystemMintsystemPurplesystemGreensystemYellowsystemOrangesystemPinksystemRedsystemTealsystemIndigoscrubberTexturedBackgroundtextBackgroundColorcontrolTextColorquaternaryLabelColorfindHighlightColorhighlightColorshadowColorwindowFrameTextColorwindowBackgroundColorkeyboardFocusIndicatorColorseparatorColorselectedControlColorcontrolBackgroundColorsecondaryLabelColortertiaryLabelColorgridColoralternateSelectedControlTextColorunemphasizedSelectedContentBackgroundColortextColorsystemBrownselectedContentBackgroundColorselectedTextColorlabelColorplaceholderTextColorunemphasizedSelectedTextBackgroundColordisabledControlTextColorheaderTextColorlinkColorselectedTextBackgroundColorunemphasizedSelectedTextColorcontrolColorselectedControlTextColorunderPageBackgroundColorselectedMenuItemTextColor

要求

安装 (Pakage.swift)

dependencies: [
    .package(url: "https://github.com/0xWDG/Colors.git", branch: "main"),
],
targets: [
    .target(name: "MyTarget", dependencies: [
        .product(name: "Colors", package: "Colors"),
    ]),
]

安装 (Xcode)

  1. 在 Xcode 中,打开你的项目并导航到 FileSwift PackagesAdd Package Dependency...
  2. 粘贴仓库 URL (https://github.com/0xWDG/Colors) 并点击 Next
  3. 点击 Finish

用法

import SwiftUI
import Colors

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Hello, World!")
                .foregroundColor(Color.disabledControlTextColor)
        }
        .padding()
    }
}

从 UIColor/NSColor 中提取颜色

使用此方法向 BaseColorColor 扩展添加新的/缺失的颜色。

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)
)

如何添加新颜色

  1. 将颜色添加到 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)
    )
  2. 将颜色添加到 Color 扩展。
    • 尽可能使用原生的 ColorNSColorUIColor.colorName
    • 在需要的地方添加 #if os(iOS) / #if os(macOS)。
    • 示例 (几乎适用于所有版本)
     /// 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
     }
    • 示例 2 (适用于特定的 iOS/macOS 版本)
    /// 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 的信息吗? 查看我的博客