iOS --- UI系列之UIColoer

xiaoxiao2021-02-28  87

开发中颜色如何表示

颜色是一个App很重要的内容。在iOS中颜色有几种表现形式:6位16进制、颜色关键字、RGB 附带一个RGB颜色查询对照表(6位16进制)http://www.114la.com/other/rgb.htm


这里是常用的颜色的方法取自UIColoer类中

+ (UIColor *)colorWithWhite:(CGFloat)white alpha:(CGFloat)alpha; //指定HSB,参数是:色调(hue),饱和的(saturation),亮度(brightness) + (UIColor *)colorWithHue:(CGFloat)hue saturation:(CGFloat)saturation brightness:(CGFloat)brightness alpha:(CGFloat)alpha; //指定RGB,参数是:红、绿、黄、透明度,范围是0-1 + (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha; //将一个CGColoer转换为一个UIColoer + (UIColor *)colorWithCGColor:(CGColorRef)cgColor; // + (UIColor *)colorWithPatternImage:(UIImage *)image; // + @property(class, nonatomic, readonly) UIColor *blackColor; // 0.0 white @property(class, nonatomic, readonly) UIColor *darkGrayColor; // 0.333 white @property(class, nonatomic, readonly) UIColor *lightGrayColor; // 0.667 white @property(class, nonatomic, readonly) UIColor *whiteColor; // 1.0 white @property(class, nonatomic, readonly) UIColor *grayColor; // 0.5 white @property(class, nonatomic, readonly) UIColor *redColor; // 1.0, 0.0, 0.0 RGB @property(class, nonatomic, readonly) UIColor *greenColor; // 0.0, 1.0, 0.0 RGB @property(class, nonatomic, readonly) UIColor *blueColor; // 0.0, 0.0, 1.0 RGB @property(class, nonatomic, readonly) UIColor *cyanColor; // 0.0, 1.0, 1.0 RGB @property(class, nonatomic, readonly) UIColor *yellowColor; // 1.0, 1.0, 0.0 RGB @property(class, nonatomic, readonly) UIColor *magentaColor; // 1.0, 0.0, 1.0 RGB @property(class, nonatomic, readonly) UIColor *orangeColor; // 1.0, 0.5, 0.0 RGB @property(class, nonatomic, readonly) UIColor *purpleColor; // 0.5, 0.0, 0.5 RGB @property(class, nonatomic, readonly) UIColor *brownColor; // 0.6, 0.4, 0.2 RGB @property(class, nonatomic, readonly) UIColor *clearColor; // 0.0 white, 0.0 alpha #else + (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

这里给出一个转换16位颜色的方法

#defineUIColorFromHex(s) [UIColor colorWithRed:(((s & 0xFF0000) >> 16))/255.0green:(((s &0xFF00) >>8))/255.0blue:((s &0xFF))/255.0alpha:1.0] self.view.backgroundColor=UIColorFromHex(0xdcdcdc);

添加一个背景图给UIView

如果想要为UIView添加一张背景图,常用的方式有两种:

第一种, 是在UIView上加载一UIImageView(在UIImageView添加背景图);

第二种, 是调用UIColor 的initWithPatternImage方法,具体做法:

UIColor *coloer = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:ImageName]]; self.view.backgroundColor = coloer;

但是第二种方法网上说有弊端,很占内存,大约比第一种方法多占6M左右而且还不会自动释放。


CGColoer UIColoer CIColoer

这里就不过多的解释了,这里有一篇文章写的很全面http://www.awnlab.com/archives/2666.html


渐变颜色如何做

可以参考一下这篇博客写的很全面很好http://www.jianshu.com/p/3e0e25fd9b85

转载请注明原文地址: https://www.6miu.com/read-50430.html

最新回复(0)