对于开发文档而言,但部分手机移动项目 所标注的色值都是 RGB颜色,词对于Android开发人员来说十分方便尔对于iOS开发人员来说十分麻烦
OC是基于C的扩张,因此C语言在OC之中可以无缝的调用,对于转换可以采用 位移 和 与 来实现完美转换
1.采用宏定义
#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]
2.采用类别
@interface UIColor (ColorFromRGB)
/*
示例: 使用十六进制的颜色 0x000000~0xffffff
self.view.backgroundColor = [UIColor colorFromRGB:0x0000ff];
*/
+(UIColor *) colorFromRGB:(int) RGBValue;
-(UIColor *) colorFromRGB:(int) RGBValue ;
-(UIColor *) colorFromRGB:(int) RGBValue alpha:(CGFloat) alpha;
@end
@implementation UIColor (ColorFromRGB)
+(UIColor *)colorFromRGB:(int)RGBValue
{
return [UIColor colorWithRed:(float)((RGBValue&0xFF0000)>>16)/255.0 green:(float)((RGBValue&0xFF00)>>8)/255.0 blue:(float)((RGBValue&0xFF))/255.0 alpha:1.0f];
}
-(UIColor *)colorFromRGB:(int)RGBValue alpha:(CGFloat)alpha
{
return [UIColor colorWithRed:(float)((RGBValue&0xFF0000)>>16)/255.0 green:(float)((RGBValue&0xFF00)>>8)/255.0 blue:(float)((RGBValue&0xFF))/255.0 alpha:alpha];
}
-(UIColor *)colorFromRGB:(int)RGBValue
{
return [self colorFromRGB:RGBValue alpha:1.0f];
}
@end