一个面试题:使用内联函数把@“#ff3344”转成UIColor
一个方便的转换:
- #define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
- - (UIColor *) stringTOColor:(NSString *)str
- {
- if (!str || [str isEqualToString:@""]) {
- return nil;
- }
- unsigned red,green,blue;
- NSRange range;
- range.length = 2;
- range.location = 0;
- [[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&red];
- range.location = 2;
- [[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&green];
- range.location = 4;
- [[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&blue];
- UIColor *color= [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:1];
- return color;
- }