uicolor 扩充

  1. - (NSString *) stringFromColor;
  2. - (NSString *) hexStringFromColor;
  3. + (UIColor *) colorWithString: (NSString *) stringToConvert;
  4. + (UIColor *) colorWithHexString: (NSString *) stringToConvert;
  1. - (NSString *) stringFromColor
  2. {
  3. NSAssert (self.canProvideRGBComponents, @"Must be a RGB color to use stringFromColor");
  4. return [NSString stringWithFormat:@"{%0.3f, %0.3f, %0.3f, %0.3f}", self.red, self.green, self.blue, self.alpha];
  5. }
  6. - (NSString *) hexStringFromColor
  7. {
  8. NSAssert (self.canProvideRGBComponents, @"Must be a RGB color to use hexStringFromColor");
  9. CGFloat r, g, b;
  10. r = self.red;
  11. g = self.green;
  12. b = self.blue;
  13. // Fix range if needed
  14. if (r < 0.0f) r = 0.0f;
  15. if (g < 0.0f) g = 0.0f;
  16. if (b < 0.0f) b = 0.0f;
  17. if (r > 1.0f) r = 1.0f;
  18. if (g > 1.0f) g = 1.0f;
  19. if (b > 1.0f) b = 1.0f;
  20. // Convert to hex string between 0x00 and 0xFF
  21. return [NSString stringWithFormat:@"%02X%02X%02X",
  22. (int)(r * 255), (int)(g * 255), (int)(b * 255)];
  23. }
  24. + (UIColor *) colorWithString: (NSString *) stringToConvert
  25. {
  26. NSString *cString = [stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  27. // Proper color strings are denoted with braces
  28. if (![cString hasPrefix:@"{"]) return DEFAULT_VOID_COLOR;
  29. if (![cString hasSuffix:@"}"]) return DEFAULT_VOID_COLOR;
  30. // Remove braces
  31. cString = [cString substringFromIndex:1];
  32. cString = [cString substringToIndex:([cString length] - 1)];
  33. CFShow(cString);
  34. // Separate into components by removing commas and spaces
  35. NSArray *components = [cString componentsSeparatedByString:@", "];
  36. if ([components count] != 4) return DEFAULT_VOID_COLOR;
  37. // Create the color
  38. return [UIColor colorWithRed:[[components objectAtIndex:0] floatValue]
  39. green:[[components objectAtIndex:1] floatValue]
  40. blue:[[components objectAtIndex:2] floatValue]
  41. alpha:[[components objectAtIndex:3] floatValue]];
  42. }
  43. + (UIColor *) colorWithHexString: (NSString *) stringToConvert
  44. {
  45. NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
  46. // String should be 6 or 8 characters
  47. if ([cString length] < 6) return DEFAULT_VOID_COLOR;
  48. // strip 0X if it appears
  49. if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
  50. if ([cString length] != 6) return DEFAULT_VOID_COLOR;
  51. // Separate into r, g, b substrings
  52. NSRange range;
  53. range.location = 0;
  54. range.length = 2;
  55. NSString *rString = [cString substringWithRange:range];
  56. range.location = 2;
  57. NSString *gString = [cString substringWithRange:range];
  58. range.location = 4;
  59. NSString *bString = [cString substringWithRange:range];
  60. // Scan values
  61. unsigned int r, g, b;
  62. [[NSScanner scannerWithString:rString] scanHexInt:&r];
  63. [[NSScanner scannerWithString:gString] scanHexInt:&g];
  64. [[NSScanner scannerWithString:bString] scanHexInt:&b];
  65. return [UIColor colorWithRed:((float) r / 255.0f)
  66. green:((float) g / 255.0f)
  67. blue:((float) b / 255.0f)
  68. alpha:1.0f];
  69. }




http://arstechnica.com/apple/guides/2009/02/iphone-development-accessing-uicolor-components.ars

开源的类

https://github.com/ars/uicolor-utilities.git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值