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 })")
}
BlackLabsSwiftUIColor
Installation
Swift Package Manager
If you are using Xcode 11 or later:
File
Swift Packages
Add Package Dependency...
UserDefaults
Works with
UIColor
andColor
.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
usesUserDefaults.standard.uicolor(forKey)
andUserDefaults.standard.uicolors(forKey)
API.Example project
Example project demonstrating usage.