如何从十六进制字符串创建 UIColor? [英] How can I create a UIColor from a hex string?
本文介绍了如何从十六进制字符串创建 UIColor?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从十六进制字符串格式创建UIColor
,例如#00FF00
?
How can I create a UIColor
from a hexadecimal string format, such as #00FF00
?
推荐答案
我发现最简单的方法是使用宏.只需将它包含在您的标题中,它就可以在您的整个项目中使用.
I've found the simplest way to do this is with a macro. Just include it in your header and it's available throughout your project.
#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]
此代码的格式化版本:
#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);
斯威夫特:
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)
}
这篇关于如何从十六进制字符串创建 UIColor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文