UIColor转十六进制字符串 方法一:经过测试发现部分颜色会转化为#fffff(五个f),转化残缺导致最后颜色无法还原,不推荐使用。 // UIColor转#ffffff格式的字符串
+ (NSString *)hexFromUIColor:(UIColor *)color
{
if (CGColorGetNumberOfComponents(color.CGColor) < ) {
const CGFloat *components = CGColorGetComponents(color.CGColor);
color = [UIColor colorWithRed:components[]
green:components[]
blue:components[]
alpha:components[]];
} if (CGColorSpaceGetModel(CGColorGetColorSpace(color.CGColor)) != kCGColorSpaceModelRGB) {
return [NSString stringWithFormat:@"#FFFFFF"];
} return [NSString stringWithFormat:@"#%x%x%x", (int)((CGColorGetComponents(color.CGColor))[]*255.0),
(int)((CGColorGetComponents(color.CGColor))[]*255.0),
(int)((CGColorGetComponents(color.CGColor))[]*255.0)];
} 方法二:完美转化为#ffffff格式,暂未发现问题,推荐使用。 // UIColor转#ffffff格式的字符串
+ (NSString *)hexStringFromColor:(UIColor *)color {
const CGFloat *components = CGColorGetComponents(color.CGColor); CGFloat r = components[];
CGFloat g = components[];
CGFloat b = components[]; return [NSString stringWithFormat:@"#%02lX%02lX%02lX",
lroundf(r * ),
lroundf(g * ),
lroundf(b * )];
} 十六进制字符串转UIColor 方法一: + (CGFloat) colorComponentFrom: (NSString *) string start: (NSUInteger) start length: (NSUInteger) length {
NSString *substring = [string substringWithRange: NSMakeRange(start, length)];
NSString *fullHex = length == ? substring : [NSString stringWithFormat: @"%@%@", substring, substring];
unsigned hexComponent;
[[NSScanner scannerWithString: fullHex] scanHexInt: &hexComponent];
return hexComponent / 255.0;
} + (UIColor *) colorWithHexString: (NSString *) hexString {
NSString *colorString = [[hexString stringByReplacingOccurrencesOfString: @"#" withString: @""] uppercaseString];
CGFloat alpha, red, blue, green;
switch ([colorString length]) {
case : // #RGB
alpha = 1.0f;
red = [self colorComponentFrom: colorString start: length: ];
green = [self colorComponentFrom: colorString start: length: ];
blue = [self colorComponentFrom: colorString start: length: ];
break;
case : // #ARGB
alpha = [self colorComponentFrom: colorString start: length: ];
red = [self colorComponentFrom: colorString start: length: ];
green = [self colorComponentFrom: colorString start: length: ];
blue = [self colorComponentFrom: colorString start: length: ];
break;
case : // #RRGGBB
alpha = 1.0f;
red = [self colorComponentFrom: colorString start: length: ];
green = [self colorComponentFrom: colorString start: length: ];
blue = [self colorComponentFrom: colorString start: length: ];
break;
case : // #AARRGGBB
alpha = [self colorComponentFrom: colorString start: length: ];
red = [self colorComponentFrom: colorString start: length: ];
green = [self colorComponentFrom: colorString start: length: ];
blue = [self colorComponentFrom: colorString start: length: ];
break;
default:
return nil;
}
return [UIColor colorWithRed: red green: green blue: blue alpha: alpha];
} 方法二: + (UIColor *)colorWithHexString:(NSString *)hexString alpha:(CGFloat)alphaValue {
NSString *cString = [[hexString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
if (cString.length < )
return [UIColor clearColor];
if ([cString hasPrefix:@"0X"])
cString = [cString substringFromIndex:];
if ([cString hasPrefix:@"#"])
cString = [cString substringFromIndex:];
if (cString.length != )
return [UIColor clearColor];
NSRange range;
range.location = ;
range.length = ;
NSString *rString = [cString substringWithRange:range];
range.location = ;
NSString *gString = [cString substringWithRange:range];
range.location = ;
NSString *bString = [cString substringWithRange:range];
unsigned int r, g, b;
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:gString] scanHexInt:&g];
[[NSScanner scannerWithString:bString] scanHexInt:&b];
// 注意:myColor是自定义宏
//#define myColor(R, G, B, A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]
return myColor(r, g, b, alphaValue);
}