對(duì)顏色的認(rèn)識(shí)
顏色是有紅色(red)綠色(green)藍(lán)色(blue)組成的癣朗。顏色值24位的顏色如:#0X123456邢隧。前面兩位12表示紅色媒吗,中間兩位34表示綠色往堡,最后兩位56表示藍(lán)色陨舱。32位的顏色值如:#0X01123456翠拣,前面的01表示透明度。后面的和24位的一樣游盲。
ios顏色的設(shè)置
- 普通色顏色設(shè)置(系統(tǒng)方法)
objc
//注意這個(gè)r误墓、g、b表示紅色益缎、綠色谜慌、藍(lán)色,且必須除以255.0莺奔。只有這樣得到浮點(diǎn)型的值
[UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a];
- 數(shù)值類的設(shè)置 alpha表示透明度的范圍:0~1
####不是字符串形式的十六進(jìn)制 支持0X123456畦娄、0x123456、 兩種格式
```objc```
+ (UIColor *)colorWithRGBHex:(UInt32)hex
{
int r = (hex >> 16) & 0xFF;
int g = (hex >> 8) & 0xFF;
int b = (hex) & 0xFF;
return [UIColor colorWithRed:r / 255.0f
green:g / 255.0f
blue:b / 255.0f
alpha:1.0f];
}
+ (UIColor *)colorWithRGBHex:(UInt32)hex alpha:(CGFloat)alpha{
int r = (hex >> 16) & 0xFF;
int g = (hex >> 8) & 0xFF;
int b = (hex) & 0xFF;
return [UIColor colorWithRed:r / 255.0f
green:g / 255.0f
blue:b / 255.0f
alpha:alpha];
}
- 顏色值是NSString的十六進(jìn)制的值
從十六進(jìn)制字符串獲取顏色 color:支持@“#123456”弊仪、 @“0X123456”熙卡、@“0x123456”、 @“123456”四種格式
objc
-
(UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha
{
//刪除字符串中的空格
NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
// String should be 6 or 8 characters
if ([cString length] < 6)
{
return [UIColor clearColor];
}
// strip 0X if it appears
//如果是0x開頭的励饵,那么截取字符串驳癌,字符串從索引為2的位置開始,一直到末尾
if ([cString hasPrefix:@"0X"]||[cString hasPrefix:@"0x"])
{
cString = [cString substringFromIndex:2];
}
//如果是#開頭的役听,那么截取字符串颓鲜,字符串從索引為1的位置開始,一直到末尾
if ([cString hasPrefix:@"#"])
{
cString = [cString substringFromIndex:1];
}
if ([cString length] != 6)
{
return [UIColor clearColor];
}// Separate into r, g, b substrings
NSRange range;
range.location = 0;
range.length = 2;
//r
NSString *rString = [cString substringWithRange:range];
//g
range.location = 2;
NSString *gString = [cString substringWithRange:range];
//b
range.location = 4;
NSString *bString = [cString substringWithRange:range];// Scan values
unsigned int r, g, b;
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:gString] scanHexInt:&g];
[[NSScanner scannerWithString:bString] scanHexInt:&b];
return [UIColor colorWithRed:((float)r / 255.0f) green:((float)g / 255.0f) blue:((float)b / 255.0f) alpha:alpha];
}
代碼具體下載地址是:https://github.com/qibingying/iosDevelopInsight/tree/master/%E9%A2%9C%E8%89%B2