1、Hex值颜色转换
#import <UIKit/UIKit.h> @interface UIColor (Extension) // 根据无符号的32位整数转换为对应的RGB颜色 + (instancetype)sy_colorWithHex:(u_int32_t)hex; @end #import "UIColor+Extension.h" @implementation UIColor (Extension) + (instancetype)sy_colorWithHex:(u_int32_t)hex{ ; ; int blue = hex & 0x0000FF; return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1.0]; } @end
2、将UIColor转换为RGB值
- (NSMutableArray *) changeUIColorToRGB:(UIColor *)color { NSMutableArray *RGBStrValueArr = [[NSMutableArray alloc] init]; NSString *RGBStr = nil; //获得RGB值描述 NSString *RGBValue = [NSString stringWithFormat:@"%@",color]; //将RGB值描述分隔成字符串 NSArray *RGBArr = [RGBValue componentsSeparatedByString:@" "]; //获取红色值 ] intValue] * ; RGBStr = [NSString stringWithFormat:@"%d",r]; [RGBStrValueArr addObject:RGBStr]; //获取绿色值 ] intValue] * ; RGBStr = [NSString stringWithFormat:@"%d",g]; [RGBStrValueArr addObject:RGBStr]; //获取蓝色值 ] intValue] * ; RGBStr = [NSString stringWithFormat:@"%d",b]; [RGBStrValueArr addObject:RGBStr]; //返回保存RGB值的数组 return [RGBStrValueArr autorelease]; }
3、16进制颜色(html颜色值)字符串转为UIColor
+(UIColor *) hexStringToColor: (NSString *) stringToConvert { NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString]; // String should be 6 or 8 characters ) return [UIColor blackColor]; // strip 0X if it appears ]; ]; ) return [UIColor blackColor]; // Separate into r, g, b substrings 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]; // 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:1.0f]; }