Skip to content

SwiftUI Color support for hex, web color constants, random, and UIColor constants.

License

Notifications You must be signed in to change notification settings

nbasham/BlackLabsSwiftUIColor

Repository files navigation

BlackLabsSwiftUIColor

Installation

Swift Package Manager

If you are using Xcode 11 or later:

  1. Click File
  2. Swift Packages
  3. Add Package Dependency...
  4. Enter: https://github.com/nbasham/BlackLabsSwiftUIColor/

UserDefaults

Works with UIColor and Color. Color is determined at runtime. This implementation grabs the current .light/.dark color and saves it, the color being saved will loose the .light/.dark color not currently in use. UIColor uses UserDefaults.standard.uicolor(forKey) and UserDefaults.standard.uicolors(forKey) API.

let color = Color.red
UserDefaults.standard.set(color, forKey: "key")
if let savedColor: Color = UserDefaults.standard.color(forKey: "key") {
print("Loaded Color from UserDefaults: \(savedColor.hex)")
}

let colors: [Color] = [.red, .orange, .pink]
UserDefaults.standard.set(colors, forKey: "arraykey")
if let savedColors: [Color] = UserDefaults.standard.colors(forKey: "arraykey") {
print("Loaded [Color] from UserDefaults: \(savedColors.map { $0.hexWithAlpha })")
}

Example project

Example project demonstrating usage.

Readme image