Fernando Valente
Fernando Valente

Reputation: 1096

Draw NSShadow inside a NSView

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!

Upvotes: -1

Views: 4204

Answers (1)

Caleb
Caleb

Reputation: 124997

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...
}

Upvotes: 7

Related Questions