ios - 是否可以快速获取颜色名称

标签 ios swift

我正在尝试快速从 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/

相关文章:

ios - 具有多个预览的AVCaptureSession

ios - Facebook 登录操作表

ios - 警报延迟 - iOS swift

基于 if else 语句的 iOS Realm 多重过滤器

ios - 辅助功能画外音失去了对分段 subview 的关注

ios - 打破动画。

ios - CollectionView didSelectItem 没有响应。为什么?

ios - 如何使用 segue 将字符串传递给 textView?

ios - 图像选择器委托(delegate)错误 : cannot assign value of type

ios - 将 cURL 命令行转换为 Swift cURL 不起作用