Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a new SwiftUI Color

Tags:

colors

swiftui

How can i add a SwiftUI Color? Normally on UIKit you can create your own Colors but in SwiftUI con Color struct it's not easy like UIColor, but it's not complicate.

extension UIColor {
    UIColor(red: 219/255, green: 175/255, blue: 15/255, alpha: 1.0)
}
like image 251
gandhi Mena Avatar asked Oct 23 '19 05:10

gandhi Mena


Video Answer


3 Answers

You can create a color set like this one (which is called "black")

And after in your code just create an extension of Color:

import SwiftUI

extension Color {
    static let customBlack = Color("black")
}

And when you will use this customBlack, if you turn you app in darkMode it will use the dark appearance that you have set in your colorset.

You can use it like this in your code:

Image(systemName: "person.crop.circle")
.foregroundColor(.customBlack)
like image 190
lmasneri Avatar answered Sep 27 '22 23:09

lmasneri


You can create your own SwiftUI Color with Color extension in a new file.swift

import SwiftUI

extension Color {
    public static var myCustomColor: Color { 
        return Color(UIColor(red: 219/255, green: 175/255, blue: 15/255, alpha: 1.0))
    }
}
like image 20
gandhi Mena Avatar answered Sep 27 '22 23:09

gandhi Mena


Add New color set file in Assets.xcassets and colors with name.

Code: Color("ColorName")

like image 43
Mohammed Ibrahim Avatar answered Sep 27 '22 22:09

Mohammed Ibrahim