2011-05-14 202 views

回答

16
- (NSString *)getHexStringForColor:(UIColor*)color { 
    const CGFloat *components = CGColorGetComponents(color.CGColor); 
    CGFloat r = components[0]; 
    CGFloat g = components[1]; 
    CGFloat b = components[2]; 

    return [NSString stringWithFormat:@"%02X%02X%02X", (int)(r * 255), (int)(g * 255), (int)(b * 255)]; 
} 
+2

不幸的是,這不適用於非RGB顏色,如'[UIColor blackColor]'。問題只是關於RGB顏色,但如果你想支持更多'UIColor',你可以在這裏使用ngb的方法:http://stackoverflow.com/a/13134326/211292 – ThomasW 2014-05-22 09:22:46