Skip to content

Instantly share code, notes, and snippets.

@Clarko
Last active July 24, 2020 15:40
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Clarko/df17edb84b79334c85895454f109f072 to your computer and use it in GitHub Desktop.
Save Clarko/df17edb84b79334c85895454f109f072 to your computer and use it in GitHub Desktop.
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