-1

I'm trying to draw a NSShadow on the background of a NSView. I want to use it as a replacement for NSGradient, as I need to support Mac OS X Tiger. How may I do that? I know this must be pretty easy and I must be making some mistake.

Thanks!

Fernando Valente
  • 1,096
  • 1
  • 8
  • 30

1 Answers1

7

The easiest approach may be to just set the shadow properties for the view's layer. If you have a NSView* named view, it'd be something like:

[[view layer] setShadowOpacity:0.5];

Setting the shadow opacity to something greater than 0 will make the shadow visible. The shadow drawn will be similar to the view's alpha channel, so whatever you draw in the view will have a shadow. There are several other shadow attributes that you can set, such as the blur radius. Take a look at the CALayer reference page for more.

If you must use NSShadow, then just set up a shadow before you do your drawing:

- (void)drawRect:(NSRect)rect
{
    NSShadow *shadow = [[[NSShadow alloc] init] autorelease];
    [shadow setShadowBlurRadius:3.0];
    [shadow setShadowOffset:NSMakeSize(0.0, 5.0)];
    [shadow setShadowColor:[NSColor colorWithCalibratedWhite:0.0 alpha:0.6]];
    [shadow set];
    // continue with your drawing...
}
iain
  • 5,660
  • 1
  • 30
  • 51
Caleb
  • 124,013
  • 19
  • 183
  • 272