如何将RGB转换为HTML颜色?
答
- (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)];
}
不幸的是,这不适用于非RGB颜色,如'[UIColor blackColor]'。问题只是关于RGB颜色,但如果你想支持更多'UIColor',你可以在这里使用ngb的方法:http://stackoverflow.com/a/13134326/211292 – ThomasW 2014-05-22 09:22:46