Jake
Jake

Reputation: 13753

Supported default light/dark mode colors for Color vs UIColor

I'm trying to use Apple’s built in colors (UIColor.label, UIColor.secondaryLabel, etc), but I can't seem to find their Color equivalent. Since I can't use them as a Color, I can't use them in my SwiftUI code. Is there any way to use these colors in SwiftUI?

Upvotes: 1

Views: 483

Answers (1)

Jake
Jake

Reputation: 13753

I was able to modify some code I found here to make solution. Not the best solution in the world, but it appears to work.

extension Color {
    static var label = Color.from(uicolor: .label)

    static func from(uicolor: UIColor) -> Color {
        var red: CGFloat = 0
        var green: CGFloat = 0
        var blue: CGFloat = 0
        var alpha: CGFloat = 0

        uicolor.getRed(&red, green: &green, blue: &blue, alpha: &alpha)

        return Color(red: Double(red), green: Double(green), blue: Double(blue)).opacity(Double(alpha))
    }
}

Usage:

Text("Test").color(.label)

Upvotes: 3

Related Questions