12

がありNSBezierPath、パス内にインセット シャドウ (Photoshop に似ています) を描画したいと考えています。

とにかくこれを行うことはありますか?また、パスを使用できることは知っていますが-stroke、パスの内側をストロークできますか (Photoshop の内側をストロークするのと同様)?

アップデート 3

static NSImage * graydient = nil;

if (!graydient) {
    graydient = [[NSImage alloc] initWithSize: NSMakeSize(22, 22)];
    [graydient lockFocus];

    NSGradient * gradient = [[NSGradient alloc] initWithColorsAndLocations: clr(@"#262729"), 0.0f, clr(@"#37383a"), 0.43f, clr(@"#37383a"), 1.0f, nil];
    [gradient drawInRect: NSMakeRect(0, 4.179, 22, 13.578) angle: 90.0f];
    [gradient release];

    [graydient unlockFocus];
}

NSColor * gcolor = [NSColor colorWithPatternImage: graydient];

[gcolor set];

NSShadow * shadow = [NSShadow new];
[shadow setShadowColor: [NSColor colorWithDeviceWhite: 1.0f alpha: 1.0f]];
[shadow setShadowBlurRadius: 0.0f];
[shadow setShadowOffset: NSMakeSize(0, 1)];
[shadow set];

[path fill];

[NSGraphicsContext saveGraphicsState];

[[path pathFromIntersectionWithPath: [NSBezierPath bezierPathWithRect: NSInsetRect([path bounds], 0.6, 0)]] setClip];

[gcolor set];

[shadow setShadowOffset: NSMakeSize(0, 1)];
[shadow setShadowColor: [NSColor blackColor]];
[shadow set];

[outer stroke];

[NSGraphicsContext restoreGraphicsState];

[NSGraphicsContext saveGraphicsState];

[[NSGraphicsContext currentContext] setCompositingOperation: NSCompositeSourceOut];

[shadow set];
[[NSColor whiteColor] set];
[inner fill];

[shadow set];
[inner fill];

[NSGraphicsContext restoreGraphicsState];

最終結果これが私の最終結果です。かなり良さそうです。それを機能させるには、シャドウを White @ 1.0 Alpha に変更する必要がありました。メニュー バー アイテムのシャドウ アルファ ノルムは 0.5 ですが、見た目は悪くありません。

Joshua Nozziに感謝します。

4

1 に答える 1

25

これは、ベジエ パスにクリップを設定してマスクとして使用し、シャドウをストロークしてから、必要に応じて通常のストロークを追加することで実現できると思います。

更新されたコードに基づく更新:

どうぞ。今夜は先延ばし気味です。:-)

// Describe an inset rect (adjust for pixel border)
NSRect whiteRect = NSInsetRect([self bounds], 20, 20);
whiteRect.origin.x += 0.5;
whiteRect.origin.y += 0.5;

// Create and fill the shown path
NSBezierPath * path = [NSBezierPath bezierPathWithRect:whiteRect];
[[NSColor whiteColor] set];
[path fill];

// Save the graphics state for shadow
[NSGraphicsContext saveGraphicsState];

// Set the shown path as the clip
[path setClip];

// Create and stroke the shadow
NSShadow * shadow = [[[NSShadow alloc] init] autorelease];
[shadow setShadowColor:[NSColor redColor]];
[shadow setShadowBlurRadius:10.0];
[shadow set];
[path stroke];

// Restore the graphics state
[NSGraphicsContext restoreGraphicsState];

// Add a nice stroke for a border
[path setLineWidth:1.0];
[[NSColor grayColor] set];
[path stroke];
于 2010-11-17T16:34:03.163 に答える