平时写界面的时候要为控件设置各种各样的颜色,然而UIColor中只封装了如下这些颜色。
+ (UIColor *)blackColor; // 0.0 white
+ (UIColor *)darkGrayColor; // 0.333 white
+ (UIColor *)lightGrayColor; // 0.667 white
+ (UIColor *)whiteColor; // 1.0 white
+ (UIColor *)grayColor; // 0.5 white
+ (UIColor *)redColor; // 1.0, 0.0, 0.0 RGB
+ (UIColor *)greenColor; // 0.0, 1.0, 0.0 RGB
+ (UIColor *)blueColor; // 0.0, 0.0, 1.0 RGB
+ (UIColor *)cyanColor; // 0.0, 1.0, 1.0 RGB
+ (UIColor *)yellowColor; // 1.0, 1.0, 0.0 RGB
+ (UIColor *)magentaColor; // 1.0, 0.0, 1.0 RGB
+ (UIColor *)orangeColor; // 1.0, 0.5, 0.0 RGB
+ (UIColor *)purpleColor; // 0.5, 0.0, 0.5 RGB
+ (UIColor *)brownColor; // 0.6, 0.4, 0.2 RGB
+ (UIColor *)clearColor; // 0.0 white, 0.0 alpha
有时候想调用一些图片上看到的颜色就很不方便。
我的解决办法是先用PhotoShop中的吸管工具吸取图片上的颜色,#02ffe6是用十六进制表示的RPG颜色,所以要先转化为10进制。02=2,ff=255,e6=230,然后就可以写上自定义颜色的方法[UIColor colorWithRed:2 / 255.0 green:255 / 255.0 blue:230 / 255.0 alpha:1.0].alpha是透明度,每个颜色的取值范围都是0 - 1所以这边要转化下,255是16进制的最大值。其实这些16进制的转化PS上已经做好了,R,B,G对应的值就是最后要用的十进制的值。
UIColor 、CIColor和CGColor 之间都是可以转换的,而且也挺方便的,我个人觉得掌握一个就可以了,在用其它的时候转化下。
比如CGColorRef btnColor = [UIColor colorWithRed:2 / 255.0 green:255 / 255.0 blue:230 / 255.0 alpha:1.0].CGColor;