gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 08:16:14 27 4
gpt4 key购买 nike

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com