可以在CGContextSetRGBFillColor中给出不同的颜色?

我怎样才能给CGContextSetRGBFillColor颜色HEXCOLOR(0xe3f3fbff)

尝试

 CGContextSetRGBFillColor(context, 0xe3 / 255.0, 0xf3 / 255.0, 0xfb / 255.0, 0xff / 255.0); 

CGContextSetRGBFillColor需要R,G,B和alpha(从0.0到1.0)的CGFloat值,所以您必须将hex颜色的每个组件都转换为0.0和1.0之间的值。

在你的情况下:

 // R = 0xe3 / 0xff = 0.890 // G = 0xf3 / 0xff = 0.953 // B = 0xfb / 0xff = 0.984 // A = 0xff / 0xff = 1.000 CGContextSetRGBFillColor(context,0.89,0.953,0.984,1.0); 

你可以将你的hex颜色string转换为r,g,b,这样的值:

 NSString *color = @"0xe3f3fbff"; unsigned r,g,b,a; [[NSScanner scannerWithString:[color substringWithRange:NSMakeRange(2,2)]] scanHexInt:&r]; [[NSScanner scannerWithString:[color substringWithRange:NSMakeRange(4,2)]] scanHexInt:&g]; [[NSScanner scannerWithString:[color substringWithRange:NSMakeRange(6,2)]] scanHexInt:&b]; [[NSScanner scannerWithString:[color substringWithRange:NSMakeRange(7,2)]] scanHexInt:&a]; CGContextSetRGBFillColor(context,r/255.0,g/255.0,b/255.0,a/255.0);