Is it possible to get the color name in quick

I'm trying to get the name of a color from a UIButton in swift, not as a value, is there a way to do this. Thanks to

I am using tintColor to set the value to get the value

    clickButton.tintColor = UIColor.blue

    var color = clickButton.tintColor

      

When I print the color value I get (UIExtendedSRGBColorSpace 0 0 1 1) anyway I can get blue instead of the value

+3


source to share


3 answers


You cannot get the "human readable" name UIColor

using the builtin. However, you can get the values RGB

as described in this post .

If you really want to get the name of the color, you can create your own dictionary, as @BoilingFire pointed out in his answer:



var color = clickButton.tintColor!     // it is set to UIColor.blue
var colors = [UIColor.red:"red", UIColor.blue:"blue", UIColor.black:"black"]  // you should add more colors here, as many as you want to support.
var colorString = String()

if colors.keys.contains(color){
    colorString = colors[color]!
}

print(colorString)     // prints "blue"

      

+1


source


Add this extension to your project

extension UIColor {
    var name: String? {
        switch self {
        case UIColor.black: return "black"
        case UIColor.darkGray: return "darkGray"
        case UIColor.lightGray: return "lightGray"
        case UIColor.white: return "white"
        case UIColor.gray: return "gray"
        case UIColor.red: return "red"
        case UIColor.green: return "green"
        case UIColor.blue: return "blue"
        case UIColor.cyan: return "cyan"
        case UIColor.yellow: return "yellow"
        case UIColor.magenta: return "magenta"
        case UIColor.orange: return "orange"
        case UIColor.purple: return "purple"
        case UIColor.brown: return "brown"
        default: return nil
        }
    }
}

      



Now you can write

print(UIColor.red.name) // Optional("red")

      

+3


source


I don't think this is possible, but you could create your own dictionary and search for the key that matches that colored object. No color would have any name.

var colors = ["blue": UIColor.blue, ...]

      

+1


source







All Articles