RGB与十六进制互转
http://tool.css-js.com/rgba.html
十六进颜色 (#03a9f4 0x050301)
+(UIColor *)becomeHexColor:(NSString *)hexColor
NSString *cString = [[hexColorstringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet]]uppercaseString];
if ([cString length] < 6) return [UIColor blackColor];
if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];
if ([cString length] != 6) return [UIColor blackColor];
NSRange range;
range.location = 0;
range.length = 2;
NSString *rColorValue = [cString substringWithRange:range];
range.location = 2;
NSString *gColorValue = [cString substringWithRange:range];
range.location = 4;
NSString *bColorValue = [cString substringWithRange:range];
unsigned int r, g, b;
[[NSScanner scannerWithString:rColorValue] scanHexInt:&r];
[[NSScanner scannerWithString:gColorValue] scanHexInt:&g];
[[NSScanner scannerWithString:bColorValue] scanHexInt:&b];
UIColor *color = [UIColor colorWithRed:((CGFloat) r / 255.0f) green:((CGFloat) g / 255.0f) blue:((CGFloat) b / 255.0f) alpha:1.0f];
return color;
}
本文介绍了一种将十六进制颜色值转换为RGB颜色值的方法,并提供了一个Objective-C函数实现。通过解析十六进制字符串并将其转换为对应的红、绿、蓝三文鱼数值,进而创建出UIColor对象。
2626

被折叠的 条评论
为什么被折叠?



