Skip to content

Instantly share code, notes, and snippets.

@jad
Created May 2, 2009 17:48
Show Gist options
  • Save jad/105640 to your computer and use it in GitHub Desktop.
Save jad/105640 to your computer and use it in GitHub Desktop.
Simple example of drawing a drop shadow with Core Graphics.
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
rect = [self bounds];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
// use the same background color as the view, but with an alpha of 1.0
const CGFloat * colorComps =
CGColorGetComponents(self.backgroundColor.CGColor);
CGContextSetRGBFillColor(context, colorComps[0], colorComps[1],
colorComps[2], 1.0);
// set the shadow
CGContextSetShadow(context, CGSizeMake(0.0, -5.0), 10.0);
CGRect rectToDraw = CGRectMake(
rect.origin.x,
rect.origin.y,
rect.size.width,
rect.size.height - 30.0);
// draw the rect, including the shadow
CGContextFillRect(context, rectToDraw);
CGContextRestoreGState(context);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment