Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SwiftUI Color Extension
//
// ConvenientColors.swift
//
// Created by Clarko on 7/15/20.
//
// Color.primary and Color.secondary are great for foreground text
// that adapts in light & dark mode, but there are a lot more
// UI Element Colors in iOS and macOS:
//
// https://developer.apple.com/documentation/uikit/uicolor/ui_element_colors
//
// https://developer.apple.com/documentation/appkit/nscolor/ui_element_colors
//
// Here are my shortcuts to a handful of common colors,
// with idiomatic names.
import SwiftUI
extension Color {
#if os(iOS)
// Foreground
static var tertiary: Color {
Color(.tertiaryLabel)
}
// Backgrounds
static var primaryBackground: Color {
Color(.systemBackground)
}
static var secondaryBackground: Color {
Color(.secondarySystemBackground)
}
static var tertiaryBackground: Color {
Color(.tertiarySystemBackground)
}
// GroupedListStyle and Form Backgrounds
static var primaryGroupedBackground: Color {
Color(.systemGroupedBackground)
}
static var secondaryGroupedBackground: Color {
Color(.secondarySystemGroupedBackground)
}
static var tertiaryGroupedBackground: Color {
Color(.tertiarySystemGroupedBackground)
}
#elseif os(macOS)
// Foreground
static var tertiary: Color {
Color(.tertiaryLabelColor)
}
// Backgrounds
static var primaryBackground: Color {
Color(.textBackgroundColor)
}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment