Nshadow com uma subclasse nsbutton
-
21-09-2019 - |
Pergunta
Aqui está o meu código:
- (void)drawRect:(NSRect)dirtyRect {
// Drawing code here.
// Create the Gradient
NSGradient *fillGradient = nil;
if (mouseIsDown)
fillGradient = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedRed:0.868 green:0.873 blue:0.868 alpha:1.000] endingColor:[NSColor colorWithCalibratedRed:0.687 green:0.687 blue:0.687 alpha:1.000]];
else
fillGradient = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedRed:0.687 green:0.687 blue:0.687 alpha:1.000] endingColor:[NSColor colorWithCalibratedRed:0.868 green:0.873 blue:0.868 alpha:1.000]];
// Add The Text
NSDictionary *att = nil;
NSMutableParagraphStyle *style =
[[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setLineBreakMode:NSLineBreakByWordWrapping];
[style setAlignment:NSLeftTextAlignment];
att = [[NSDictionary alloc] initWithObjectsAndKeys:
style, NSParagraphStyleAttributeName,
[NSColor blackColor],
NSForegroundColorAttributeName, nil];
[style release];
// Create the path
aPath = [[NSBezierPath bezierPath] retain];
[aPath moveToPoint:NSMakePoint(10.0, 0.0)];
[aPath lineToPoint:NSMakePoint(70.0, 0.0)];
[aPath lineToPoint:NSMakePoint(70.0, 23.0)];
[aPath lineToPoint:NSMakePoint(10.0, 23.0)];
[aPath lineToPoint:NSMakePoint(0.0, 10.0)];
NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowColor:[NSColor blackColor]];
[shadow setShadowOffset:NSMakeSize(0, 0)];
[shadow setShadowBlurRadius:5];
[shadow set];
NSRect rect;
rect.size = [[self title] sizeWithAttributes:att];
rect.origin.x = floor( NSMidX([self bounds]) - rect.size.width / 2 - 8);
rect.origin.y = floor( NSMidY([self bounds]) - rect.size.height / 2 - 5);
[fillGradient drawInBezierPath:aPath angle:90.0];
[fillGradient release];
[[self title] drawInRect:rect withAttributes:att];
[att release];
} O problema é que o nshadow está por trás do texto e não o NSBezierPath, 'Apath', como eu adicionaria a sombra o NSBEZIERPATH?
Solução
Tente embrulhar as coisas de preenchimento de caminho nshadow e bezier (por exemplo, de aPath = [[NSBezierPath bezierPath] retain];
para [fillGradient release];
) dentro [NSGraphicsContext saveGraphicsState]
e [NSGraphicsContext restoreGraphicsState]
.
Além disso, a sombra pode ser cortada pelos limites da vista (não me lembro se isso é verdade ou não).
(Além disso, a maneira "mais adequada" de fazer isso provavelmente seria subclasse NSButtonCell
também, substituindo drawWithFrame:inView:
/-drawInteriorWithFrame:inView:
e devolver sua classe de célula personalizada do seu botão +cellClass
Método (certificando -se de definir a classe celular adequada também no IB ou trocá -la no botão -initWithCoder:
). A maneira como você está fazendo isso pode atender às suas necessidades bem, no entanto.)