CGColorRef and the text shadow


Can someone kindly explain this line of code to me:

CGColorRef color = [[UIColor darkGrayColor] CGColor];

It appears that it is creating a new UIColor Object and sending it the message CGColor? I’m confused.

Thank you!


Yes, the code evaluates to something like this:

UIColor *clr =  [[UIColor alloc] initWithRed:0.3 green:0.3 blue:0.3 alpha:1.0];
CGColorRef cgClr = [clr CGColor];

UIColor has a bunch of convenience methods for creating common colors without having to specify the RGB values yourself.


Look at the API reference for UIColor.

[quote]The Quartz color reference that corresponds to the receiver’s color. (read-only)


That is, you send a message to the CGColor get accessor. The reason is that the next line of code in Joe’s example needs a CGColorRef. CGContextSetShadowWithColor takes a CGColorRef, not a UIColor. That’s the reason you’ll use UIColor’s CGColor accessor to retrieve it.


Thank you guys. I am sorry for the delayed response. I got busy with school and had to put the book down for a week or so. You guys are generous enough to respond in a timely fashion (and on a Saturday!) so I do like to follow up my posts with an acknowledgment and a thank you.