在新的 SwiftUI 中,类型与fromColor
非常相似。UIColor
UIKit
正如预期的那样,有一些常见的颜色,但我注意到另外两种颜色:
.primary
.secondary
Apple 文档中没有关于不同Color
s 的描述。
- 这些颜色是什么?
- 对于某些事情,我应该使用哪一个?
与UIColor
. 主要和次要是指文本颜色,它们是UIColor.label
和UIColor.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..
}
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)
}
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)]
}
}
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)
}
在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 的一个示例。
如果你倒带一点,动机也解释得非常清楚和很好的例子。
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)
}
我们可以通过提供这样的扩展来更改.primary
和颜色:.secondary
Color
extension Color {
public static let primary = Color("SomeCustomColor") //Color that we have set in Assets catalog
public static let secondary = Color.green
}
我也一直在探索这个。似乎.primary
设置为“环境的强调色”。所以我最好的猜测是它是基于平台的系统设置,可能会受到特定主题(如暗模式)的影响。仍然不确定您是否可以像 Android 主题一样自行更改它。在 iOS 13 中使用.primary
会呈现浅蓝色。
目前关于这些细节的文档似乎相当稀少。