如何将十六进制颜色数值转换为UIColor呢?

如题,如何将十六进制颜色数值转换为UIColor呢?
2024-11-16 04:51:40
推荐回答(3个)
回答1:

1. 定义一个转换颜色的宏。

#define UIColorFromRGBA(rgbValue, alphaValue) \

[UIColor \

colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \

green:((float)((rgbValue & 0x00FF00) >> 8))/255.0 \

blue:((float)(rgbValue & 0x0000FF))/255.0 \

alpha:alphaValue]

2. 随时随地使用

UICorlor *color = UIColorFromRGBA(0xFF0000, .75);

回答2:

楼上的是16进制表示法,下面是rgb表示法的转换,大同小异。#define UIColorFromRGB2(r, g, b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0] 查看原帖>>

回答3:

//RGB color macro#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]//RGB color macro with alpha#define UIColorFromRGBWithAlpha(rgbValue,a) [UIColor \colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \blue:((float)(rgbValue & 0xFF))/255.0 alpha:a] 查看原帖>>