十六进制颜色转换

来源:互联网 发布:17网络批发市场新潮都 编辑:程序博客网 时间:2024/05/22 01:48

一个面试题:使用内联函数把@“#ff3344”转成UIColor

一个方便的转换:
  1. #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]  

[cpp] view plain cop

  1. - (UIColor *) stringTOColor:(NSString *)str  
  2. {  
  3.     if (!str || [str isEqualToString:@""]) {  
  4.         return nil;  
  5.     }  
  6.     unsigned red,green,blue;  
  7.     NSRange range;  
  8.     range.length = 2;  
  9.     range.location = 0;  
  10.     [[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&red];  
  11.     range.location = 2;  
  12.     [[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&green];  
  13.     range.location = 4;  
  14.     [[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&blue];  
  15.     UIColor *color= [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:1];  
  16.     return color;  
  17. }  
0 0
原创粉丝点击