8

I am trying to get color name from UIButton in swift instead of value is there any way to do that. Thanks

I am using tintColor to set value as well to get value

    clickButton.tintColor = UIColor.blue

    var color = clickButton.tintColor

when I am printing color value I get (UIExtendedSRGBColorSpace 0 0 1 1) is there anyway I can get blue instead of value

Punya
  • 333
  • 2
  • 11

6 Answers6

10

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")
Luca Angeletti
  • 56,130
  • 11
  • 114
  • 144
5

You cannot get the "human-readable" name of a UIColor by using a built-in. However you can get the RGB values, as described in this post.

If you really want to get the name of the color, you can build your own dictionary, as @BoilingFire pointed out in their 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"
Mr. Xcoder
  • 4,580
  • 5
  • 25
  • 43
4

From iOS 14.0+ you can also use https://developer.apple.com/documentation/uikit/uicolor/3600314-accessibilityname

UIColor.systemRed.accessibilityName // returns "Red"
3

You can use this extension to get name of color created via Color Assets in XCode.

extension UIColor {
    /// Name of color. Only colors created with XCode Color Assets will return actual name, colors created programatically will always return nil.
    var name: String? {
        let str = String(describing: self).dropLast()
        guard let nameRange = str.range(of: "name = ") else {
            return nil
        }
        let cropped = str[nameRange.upperBound ..< str.endIndex]
        if cropped.isEmpty {
            return nil
        }
        return String(cropped)
    }
}

Result:

enter image description here

Adam
  • 1,494
  • 1
  • 14
  • 24
1

I don't think it's possible but you could build your own dictionnary and search for the key that corresponds to that color object. Not any color would have a name anyways.

var colors = ["blue": UIColor.blue, ...]
BoilingFire
  • 181
  • 1
  • 9
-1

Swift 5.5

Let's assume you have a variable Orange of type UIColor.

var orange: UIColor = UIColor(displayP3Red: 1, green: 0.5, blue: 0, alpha: 1)

You can get the color name by:

var colorName: String {
    orange.accessibilityName
}

Hope this helped. Cheers

Usefz89
  • 41
  • 4