SwiftUI 中的 .primary 和 .secondary 颜色是什么?

Geo*_*e_E 30 swift swiftui

在新的 SwiftUI 中,类型ColorUIColorfrom非常相似UIKit

正如预期的那样,有常见的颜色,但我注意到还有另外两种颜色:

  • .primary
  • .secondary

Apple 文档中没有关于不同Colors的描述。

  • 这些颜色是什么?
  • 对于某些事情,我应该使用哪一种?

sam*_*ize 41

UIColor. 主要和次要是指文本颜色,即UIColor.labelUIColor.secondaryLabel

一个简单的扩展来提供更多UIColor

public extension Color {
    static let lightText = Color(UIColor.lightText)
    static let darkText = Color(UIColor.darkText)

    static let label = Color(UIColor.label)
    static let secondaryLabel = Color(UIColor.secondaryLabel)
    static let tertiaryLabel = Color(UIColor.tertiaryLabel)
    static let quaternaryLabel = Color(UIColor.quaternaryLabel)

    static let systemBackground = Color(UIColor.systemBackground)
    static let secondarySystemBackground = Color(UIColor.secondarySystemBackground)
    static let tertiarySystemBackground = Color(UIColor.tertiarySystemBackground)

    // There are more..
}
Run Code Online (Sandbox Code Playgroud)

  • UIColor.systemBackground 的一个重要功能是它会动态适应亮/暗模式。如果我们静态设置systemBackground = Color(UIColor.systemBackground),它会像UIColor.systemBackground一样响应变化吗?或者我们需要 static var systemBackground: Color { Color(UIColor.systemBackground) } 来代替? (3认同)

Ste*_*cht 17

在2019 年 6 月 3 日更新的 iOS 和 macOS 资源下,可以找到:

所有文本样式的主要、次要、第三和第四文本样式变体

看这里:https : //developer.apple.com/design/whats-new/?id=06032019a

黑暗模式的人机界面指南部分内容如下:

使用系统提供的标签颜色作为标签。一级、二级、三级和四级标签颜色会自动适应明暗外观。有关相关指南,请参阅排版。

https://developer.apple.com/design/human-interface-guidelines/ios/visual-design/dark-mode/

最后,初步的 iOS 13 开发人员文档显示这些是预定义的 UIColors:

基本的:

包含主要内容的文本标签的颜色。

因此,secondaryLabel、thirdLabel 和quaternaryLabel 是包含二级或三级或四级内容的文本标签的颜色。

见这里:https : //developer.apple.com/documentation/uikit/uicolor/3173131-label

所以这些是用于文本标签的 UIColors。它们是不同的颜色,取决于它们是重要内容(例如标题:主要)还是描述标签(次要)等。最后,它会根据您使用的是亮模式、暗模式还是高对比度模式自动应用适当的颜色。

有一个 WWDC 2019 视频https://developer.apple.com/videos/play/wwdc2018/210/ 显示了 Apple Mail 大约 31:40 的示例。

如果你稍微回顾一下,动机也解释得非常清楚,并通过例子很好地解释了。


Far*_*ouK 14

SwiftUI 仍然缺少系统颜色,而且我没有找到使用原色和副色的正确方法,直到那时您可以使用此扩展将所有 UIColors 带到 SwiftUI:

extension Color {
     
    // MARK: - Text Colors
    static let lightText = Color(UIColor.lightText)
    static let darkText = Color(UIColor.darkText)
    static let placeholderText = Color(UIColor.placeholderText)

    // MARK: - Label Colors
    static let label = Color(UIColor.label)
    static let secondaryLabel = Color(UIColor.secondaryLabel)
    static let tertiaryLabel = Color(UIColor.tertiaryLabel)
    static let quaternaryLabel = Color(UIColor.quaternaryLabel)

    // MARK: - Background Colors
    static let systemBackground = Color(UIColor.systemBackground)
    static let secondarySystemBackground = Color(UIColor.secondarySystemBackground)
    static let tertiarySystemBackground = Color(UIColor.tertiarySystemBackground)
    
    // MARK: - Fill Colors
    static let systemFill = Color(UIColor.systemFill)
    static let secondarySystemFill = Color(UIColor.secondarySystemFill)
    static let tertiarySystemFill = Color(UIColor.tertiarySystemFill)
    static let quaternarySystemFill = Color(UIColor.quaternarySystemFill)
    
    // MARK: - Grouped Background Colors
    static let systemGroupedBackground = Color(UIColor.systemGroupedBackground)
    static let secondarySystemGroupedBackground = Color(UIColor.secondarySystemGroupedBackground)
    static let tertiarySystemGroupedBackground = Color(UIColor.tertiarySystemGroupedBackground)
    
    // MARK: - Gray Colors
    static let systemGray = Color(UIColor.systemGray)
    static let systemGray2 = Color(UIColor.systemGray2)
    static let systemGray3 = Color(UIColor.systemGray3)
    static let systemGray4 = Color(UIColor.systemGray4)
    static let systemGray5 = Color(UIColor.systemGray5)
    static let systemGray6 = Color(UIColor.systemGray6)
    
    // MARK: - Other Colors
    static let separator = Color(UIColor.separator)
    static let opaqueSeparator = Color(UIColor.opaqueSeparator)
    static let link = Color(UIColor.link)
    
    // MARK: System Colors
    static let systemBlue = Color(UIColor.systemBlue)
    static let systemPurple = Color(UIColor.systemPurple)
    static let systemGreen = Color(UIColor.systemGreen)
    static let systemYellow = Color(UIColor.systemYellow)
    static let systemOrange = Color(UIColor.systemOrange)
    static let systemPink = Color(UIColor.systemPink)
    static let systemRed = Color(UIColor.systemRed)
    static let systemTeal = Color(UIColor.systemTeal)
    static let systemIndigo = Color(UIColor.systemIndigo)

}
Run Code Online (Sandbox Code Playgroud)


Ram*_*mis 13

这是一个颜色列表,用于查看浅色和深色模式的差异。 在此处输入图片说明

用于生成调色板的代码:

var body: some View {
    VStack {
        HStack {
            Text("Light")
                .frame(width: 75, height: 40)
            Text("Dark")
                .frame(width: 75, height: 40)
        }
        List(CC.colors, id: \.name) { color in
            HStack {
                Text(color.name)
                    .frame(width: 300, alignment: .trailing)
                Rectangle()
                    .environment(\.colorScheme, .light)
                    .frame(width: 75, height: 40)
                    .foregroundColor(color.color)
                    .border(Color.black, width: 3)
                Rectangle()
                    .environment(\.colorScheme, .dark)
                    .frame(width: 75, height: 40)
                    .foregroundColor(color.color)
                    .border(Color.black, width: 3)

            }
        }
        //PlacesListView(places: placesViewModel.places, htmlAttributions: placesViewModel.htmlAttributions)
    }
    .navigationTitle("Places")
   // .embedInNavigationView()
}
Run Code Online (Sandbox Code Playgroud)

颜色列表取自FarouK 的答案。

struct CC {
    let name: String
    let color: Color

    static var colors: [CC] { [
        CC(name: "lightText", color: .lightText),
        CC(name: "darkText", color: .darkText),
        CC(name: "placeholderText", color: .placeholderText),
        CC(name: "label", color: .label),
        CC(name: "secondaryLabel", color: .secondaryLabel),
        CC(name: "tertiaryLabel", color: .tertiaryLabel),
        CC(name: "quaternaryLabel", color: .quaternaryLabel),
        CC(name: "systemBackground", color: .systemBackground),
        CC(name: "secondarySystemBackground", color: .secondarySystemBackground),
        CC(name: "tertiarySystemBackground", color: .tertiarySystemBackground),
        CC(name: "systemFill", color: .systemFill),
        CC(name: "secondarySystemFill", color: .secondarySystemFill),
        CC(name: "tertiarySystemFill", color: .tertiarySystemFill),
        CC(name: "quaternarySystemFill", color: .quaternarySystemFill),
        CC(name: "systemGroupedBackground", color: .systemGroupedBackground),
        CC(name: "secondarySystemGroupedBackground", color: .secondarySystemGroupedBackground),
        CC(name: "tertiarySystemGroupedBackground", color: .tertiarySystemGroupedBackground),
        CC(name: "systemGray", color: .systemGray),
        CC(name: "systemGray2", color: .systemGray2),
        CC(name: "systemGray3", color: .systemGray3),
        CC(name: "systemGray4", color: .systemGray4),
        CC(name: "systemGray5", color: .systemGray5),
        CC(name: "systemGray6", color: .systemGray6),
        CC(name: "separator", color: .separator),
        CC(name: "opaqueSeparator", color: .opaqueSeparator),
        CC(name: "link", color: .link),
        CC(name: "systemRed", color: .systemRed),
        CC(name: "systemBlue", color: .systemBlue),
        CC(name: "systemPink", color: .systemPink),
        CC(name: "systemTeal", color: .systemTeal),
        CC(name: "systemGreen", color: .systemGreen),
        CC(name: "systemIndigo", color: .systemIndigo),
        CC(name: "systemOrange", color: .systemOrange),
        CC(name: "systemPurple", color: .systemPurple),
        CC(name: "systemYellow", color: .systemYellow)]
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

我们可以通过提供如下扩展来更改颜色.primary.secondaryColor

extension Color {
    public static let primary = Color("SomeCustomColor") //Color that we have set in Assets catalog
    public static let secondary =  Color.green
}
Run Code Online (Sandbox Code Playgroud)

  • 从 Xcode 11.3 开始会阻止预览编译 (3认同)