2

I am drawing a shape with a stroke around it by doing following

- (void)drawRect:(CGRect)rect
{
    // Draw a cross rectagle
    CGContextRef    context     =   UIGraphicsGetCurrentContext();

    CGContextSaveGState(context);

    CGContextBeginPath(context);
    CGContextMoveToPoint    (context, 190, 0);
    CGContextAddLineToPoint (context, 220, 0);
    CGContextAddLineToPoint (context, 300, 80);
    CGContextAddLineToPoint (context, 300, 110);
    CGContextClosePath(context);

    CGContextSetFillColorWithColor(context, bgColor);                           // fill color
    CGContextSetStrokeColorWithColor(context, [UIColor grayColor].CGColor);     // set color for stroke
    CGContextSetLineWidth(context, .8);                                         // set width for stroke
    CGContextDrawPath(context,  kCGPathFillStroke);                             // do fill and stroke together


    CGContextEOClip(context);
    CGContextSetShadowWithColor(context, CGSizeMake(1, 1), 1.0, [UIColor whiteColor].CGColor);
    CGContextSetBlendMode (context, kCGBlendModeScreen);
    CGContextRestoreGState(context);
}

and what I am ending up like below ( the cross flag )

enter image description here

Now this time, I would like to drop some shadow around the cross flag as well.

What should I do to achieve this. Please advice me on this issue. Thanks.

tranvutuan
  • 6,089
  • 8
  • 47
  • 83

2 Answers2

4

CGContextSetShadow or CGContextSetShadowWithColor (documentation 1, documentation 2)

In your case, I was able to get a shadow via

...
CGContextSaveGState(context);

CGContextSetShadowWithColor(context, CGSizeMake(-3 , 2), 4.0, [UIColor whiteColor].CGColor);

CGContextBeginPath(context);
CGContextMoveToPoint    (context, 190, 0);
...

And I removed these from the bottom (clip wasn't doing anything here, why the blend mode?)

CGContextEOClip(context);
CGContextSetShadowWithColor(context, CGSizeMake(1, 1), 1.0, [UIColor whiteColor].CGColor);
CGContextSetBlendMode (context, kCGBlendModeScreen);
WDUK
  • 18,870
  • 3
  • 64
  • 72
-1

You should be able to accomplish this by tracing your flag using UIBezierPath and applying the shadow to the path.

Here is a bit of sample code that may be of use

// create highlight
UIRectCorner corners = UIRectCornerTopLeft;
UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0.0f, 0.0f, self.bounds.size.width, self.bounds.size.height + 50.0f) byRoundingCorners:corners cornerRadii:CGSizeMake(32.0f, 32.0f)];
[[self layer] setShadowPath:[shadowPath CGPath]];
[[self layer] setShadowOpacity:0.5f];
[[self layer] setShadowRadius:25.0f];
[[self layer] setShadowOffset:CGSizeMake(0.0f, 0.0f)];
[[self layer] setShadowColor:[[UIColor colorWithRed:1.0f green:1.0f blue:0.75f alpha:1.0f] CGColor]];
propstm
  • 3,461
  • 5
  • 28
  • 41
  • This is overkill, if you're already using CG to draw your paths, you might as well use CG to draw a shadow as part of that. – WDUK Nov 08 '12 at 20:40
  • @propstm: i dont know it works or not. However, when using UIBeizerPath, we should provide a rectangle which is applied shadow. In this case, I am drawing a cross flag by using `CGContextAddLineToPoint`. Please correct me if I am wrong. – tranvutuan Nov 08 '12 at 20:42
  • 1
    @ttran: UIBezierPath isn't only for rectangles. You can create a UIBezierPath with similar code to the CGContext path code in your question, and the resulting path—and shadow—will be the same. – Peter Hosey Nov 09 '12 at 02:22