gpt4 book ai didi

ios - 如何从十六进制字符串创建 UIColor?

转载 作者:IT王子 更新时间:2023-10-29 07:24:23 28 4
gpt4 key购买 nike

如何从 十六进制 字符串格式(例如 #00FF00)创建 UIColor

最佳答案

我发现最简单的方法是使用宏。只需将它包含在您的 header 中,它就可以在您的整个项目中使用。

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

uicolor macro with hex values

此代码的格式化版本:

#define UIColorFromRGB(rgbValue) \
[UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0x00FF00) >> 8))/255.0 \
blue:((float)((rgbValue & 0x0000FF) >> 0))/255.0 \
alpha:1.0]

用法:

label.textColor = UIColorFromRGB(0xBC1128);

swift :

static func UIColorFromRGB(_ rgbValue: Int) -> UIColor! {
return UIColor(
red: CGFloat((Float((rgbValue & 0xff0000) >> 16)) / 255.0),
green: CGFloat((Float((rgbValue & 0x00ff00) >> 8)) / 255.0),
blue: CGFloat((Float((rgbValue & 0x0000ff) >> 0)) / 255.0),
alpha: 1.0)
}

关于ios - 如何从十六进制字符串创建 UIColor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1560081/

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