How to convert UIColor to SwiftUI‘s Color

SwiftColorsUicolorSwiftui

Swift Problem Overview


I want to use a UIColor as foregroundcolor for an object but I don’t know how to convert UIColor to a Color

var myColor: UIColor
RoundedRectangle(cornerRadius: 5).foregroundColor(UIColor(myColor))

Swift Solutions


Solution 1 - Swift

Starting with beta 5, you can create a Color from a UIColor:

Color(UIColor.systemBlue)

Solution 2 - Swift

Both iOS and macOS

Color has a native initializer for it that takes an UIColor or NSColor as an argument:

Color(.red) /* or any other UIColor/NSColor you need INSIDE parentheses */

DO NOT call Color(UIColor.red) explicitly !!!. This will couple your SwiftUI code to the UIKit. Instead, just call Color(.red) That will infer the correct module automatically.

Also, Note this difference:

Color.red     /* this `red` is SwiftUI native `red` */
Color(.red)   /* this `red` is UIKit `red` */

suColor vs uiColor

Note that:

Color.red and UIColor.red are NOT same! They have different values and look different with each other. So DON'T assume this worth nothing

These are equal instead: SwiftUI.Color.Red == UIKit.UIColor.systemRed

Also, You can check out How to get RGB components from SwiftUI.Color


Extension

You can implement a custom variable for it to make it more like cgColor and ciColor

extension UIColor {
    /// The SwiftUI color associated with the receiver.
    var suColor: Color { Color(self) }
}

so it would be like:

UIColor.red         // UIKit color
UIColor.red.suColor // SwiftUI color
UIColor.red.cgColor // Core graphic color
UIColor.red.ciColor // Core image color

Note: Click here to see How to convert SwiftUI.Color to UIColor

Solution 3 - Swift

Using two helper extensions:

To extract components from UIColor:

extension UIColor {
    var rgba: (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {
        var red: CGFloat = 0
        var green: CGFloat = 0
        var blue: CGFloat = 0
        var alpha: CGFloat = 0
        getRed(&red, green: &green, blue: &blue, alpha: &alpha)
        
        return (red, green, blue, alpha)
    }
}

To init with UIColor:

extension Color {
    init(uiColor: UIColor) {
        self.init(red: Double(uiColor.rgba.red),
                  green: Double(uiColor.rgba.green),
                  blue: Double(uiColor.rgba.blue),
                  opacity: Double(uiColor.rgba.alpha))
    }
}

Usage:

Color(uiColor: .red)

Solution 4 - Swift

In Swift UI custom colors with a convenient extension:

extension UIColor {
struct purple {
    static let normal = UIColor(red:0.043, green:0.576 ,blue:0.588 , alpha:1.00)
    static let light = UIColor(red: 1, green: 1, blue: 1, alpha: 1)
    static let dark = UIColor(red: 1, green: 1, blue: 1, alpha: 1)
}
struct gray {
    static let normal = UIColor(red:0.5, green:0.5 ,blue:0.5 , alpha:1.00)
    static let dark = UIColor(red: 1, green: 1, blue: 1, alpha: 1)
}
}

and using this:

var body: some View {
    Text("Hello World")
    .foregroundColor(Color(UIColor.purple.normal))
    .background(Color(UIColor.gray.normal))
}

Edited with Xcode 11.0 beta 6 and the code may change

Solution 5 - Swift

I'm a really old hobbyist. Here is one way that works for me. Yes, I do use globals for reusable statements in a Constant.swift file. This example is inline so that it is easier to see. I do not say this is the way to go, it is just my old way.

Screenshot (27k)

   import SwiftUI
    
    // named color from the developer's pallet
    let aluminumColor = Color(UIColor.lightGray)
    
    // a custom color I use often
    let butterColor = Color.init(red: 0.9993399978, 
                               green: 0.9350042167, 
                               blue: 0.5304131241)
    
    // how I use this in a SwiftUI VStack:
    
    Text("Fur People")
           .font(.title)
           .foregroundColor(aluminumColor)
           .shadow(color: butterColor, radius: 4, x: 2, y: 2)
           .minimumScaleFactor(0.75)

Solution 6 - Swift

Create an extension like this:

extension Color {
    static let someColor = Color(UIColor.systemIndigo) // << Select any UIColor
}

Usage:

struct ContentView: View {
       var body: some View {
        Rectangle()
            .frame(width: 100, height: 100)
            .foregroundColor(.someColor)
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnOkView Question on Stackoverflow
Solution 1 - SwiftkontikiView Answer on Stackoverflow
Solution 2 - SwiftMojtaba HosseiniView Answer on Stackoverflow
Solution 3 - Swiftuser11701404View Answer on Stackoverflow
Solution 4 - SwiftJoannesView Answer on Stackoverflow
Solution 5 - SwiftAB MurphyView Answer on Stackoverflow
Solution 6 - SwiftHarshil PatelView Answer on Stackoverflow