- - (NSString *) stringFromColor;
- - (NSString *) hexStringFromColor;
- + (UIColor *) colorWithString: (NSString *) stringToConvert;
- + (UIColor *) colorWithHexString: (NSString *) stringToConvert;
- - (NSString *) stringFromColor
- {
- NSAssert (self.canProvideRGBComponents, @"Must be a RGB color to use stringFromColor");
- return [NSString stringWithFormat:@"{%0.3f, %0.3f, %0.3f, %0.3f}", self.red, self.green, self.blue, self.alpha];
- }
- - (NSString *) hexStringFromColor
- {
- NSAssert (self.canProvideRGBComponents, @"Must be a RGB color to use hexStringFromColor");
- CGFloat r, g, b;
- r = self.red;
- g = self.green;
- b = self.blue;
- // Fix range if needed
- if (r < 0.0f) r = 0.0f;
- if (g < 0.0f) g = 0.0f;
- if (b < 0.0f) b = 0.0f;
- if (r > 1.0f) r = 1.0f;
- if (g > 1.0f) g = 1.0f;
- if (b > 1.0f) b = 1.0f;
- // Convert to hex string between 0x00 and 0xFF
- return [NSString stringWithFormat:@"%02X%02X%02X",
- (int)(r * 255), (int)(g * 255), (int)(b * 255)];
- }
- + (UIColor *) colorWithString: (NSString *) stringToConvert
- {
- NSString *cString = [stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
- // Proper color strings are denoted with braces
- if (![cString hasPrefix:@"{"]) return DEFAULT_VOID_COLOR;
- if (![cString hasSuffix:@"}"]) return DEFAULT_VOID_COLOR;
- // Remove braces
- cString = [cString substringFromIndex:1];
- cString = [cString substringToIndex:([cString length] - 1)];
- CFShow(cString);
- // Separate into components by removing commas and spaces
- NSArray *components = [cString componentsSeparatedByString:@", "];
- if ([components count] != 4) return DEFAULT_VOID_COLOR;
- // Create the color
- return [UIColor colorWithRed:[[components objectAtIndex:0] floatValue]
- green:[[components objectAtIndex:1] floatValue]
- blue:[[components objectAtIndex:2] floatValue]
- alpha:[[components objectAtIndex:3] floatValue]];
- }
- + (UIColor *) colorWithHexString: (NSString *) stringToConvert
- {
- NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
- // String should be 6 or 8 characters
- if ([cString length] < 6) return DEFAULT_VOID_COLOR;
- // strip 0X if it appears
- if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
- if ([cString length] != 6) return DEFAULT_VOID_COLOR;
- // Separate into r, g, b substrings
- NSRange range;
- range.location = 0;
- range.length = 2;
- NSString *rString = [cString substringWithRange:range];
- range.location = 2;
- NSString *gString = [cString substringWithRange:range];
- range.location = 4;
- 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];
- }
http://arstechnica.com/apple/guides/2009/02/iphone-development-accessing-uicolor-components.ars
开源的类
https://github.com/ars/uicolor-utilities.git