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

时间:2019-06-05 18:29:11

标签: swift swiftui

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

有一些常见的颜色,如预期的那样,但是我注意到了另外两种颜色:

  • .primary
  • .secondary

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

  • 这些是什么颜色?
  • 在某些事情上我应该使用哪个?

6 个答案:

答案 0 :(得分:23)

UIColor提供的相比,SwiftUI似乎不完整。主要和次要是指文本颜色,分别是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..
}

答案 1 :(得分:6)

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)

}

答案 2 :(得分:3)

在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,tertiaryLabel和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左右的示例。

如果您倒带一点,也会通过示例非常清楚地说明动机。

答案 3 :(得分:3)

以下是颜色列表,以查看明暗模式之间的差异。 enter image description here

用于生成调色板的代码:

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

颜色列表来自 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)]
    }
}

答案 4 :(得分:1)

我也一直在探索这一点。似乎.primary设置为“环境的强调色”。因此,我最好的猜测是这是基于平台的系统设置,并且可能会受到特定主题的影响(例如黑暗模式)。仍然不确定是否可以像Android主题一样自行更改。在iOS 13中使用.primary会呈现浅蓝色。

目前,有关这些详细信息的文档似乎还很少。

答案 5 :(得分:0)

我们可以通过提供.primary的扩展名来更改.secondaryColor的颜色,如下所示:

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