我正在尝试快速从 UIButton 获取颜色名称而不是值,有什么办法可以做到这一点。谢谢
我正在使用 tintColor 来设置值以及获取值
clickButton.tintColor = UIColor.blue
var color = clickButton.tintColor
当我打印颜色值时,我得到 (UIExtendedSRGBColorSpace 0 0 1 1) 无论如何我可以得到蓝色而不是值
最佳答案
将此扩展添加到您的项目
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
}
}
}
现在你可以写了
print(UIColor.red.name) // Optional("red")
关于ios - 是否可以快速获取颜色名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44672594/