Skip to content

Instantly share code, notes, and snippets.

@konstantinbe
Last active December 13, 2015 21:19
Show Gist options
  • Save konstantinbe/4976764 to your computer and use it in GitHub Desktop.
Save konstantinbe/4976764 to your computer and use it in GitHub Desktop.
Menu style highlighting for table row views
- (void)drawSelectionInRect:(NSRect)rect {
if (self.selectionHighlightStyle == NSTableViewSelectionHighlightStyleNone) return;
NSColor *borderColorTop = [NSColor colorWithCalibratedHue:229.0 / 360.0 saturation:0.70 brightness:0.92 alpha:1.0];
NSColor *borderColorBottom = [NSColor colorWithCalibratedHue:228.5 / 360.0 saturation:0.955 brightness:0.90 alpha:1.0];
NSColor *gradientColorTop = [NSColor colorWithCalibratedHue:229.0 / 360.0 saturation:0.66 brightness:0.97 alpha:1.0];
NSColor *gradientColorBottom = [NSColor colorWithCalibratedHue:229.0 / 360.0 saturation:0.90 brightness:0.95 alpha:1.0];
NSGradient *gradient = [[NSGradient alloc] initWithColorsAndLocations:gradientColorTop, 0.0, gradientColorBottom, 1.0, nil];
const CGRect bounds = self.bounds;
const CGPoint cornerTopLeft = CGPointMake(0, 1);
const CGPoint cornerTopRight = CGPointMake(bounds.size.width, 1);
const CGPoint cornerBottomLeft = CGPointMake(0, bounds.size.height);
const CGPoint cornerBottomRight = CGPointMake(bounds.size.width, bounds.size.height);
NSGraphicsContext* graphicsContext = [NSGraphicsContext currentContext];
NSBezierPath *bezierPath = [NSBezierPath bezierPathWithRect:CGRectInset(bounds, 0, 1)];
[graphicsContext saveGraphicsState];
[bezierPath setLineWidth:1];
[gradient drawInBezierPath:bezierPath angle:90];
[graphicsContext setShouldAntialias:NO];
[borderColorTop setStroke];
[NSBezierPath strokeLineFromPoint:cornerTopLeft toPoint:cornerTopRight];
[borderColorBottom setStroke];
[NSBezierPath strokeLineFromPoint:cornerBottomLeft toPoint:cornerBottomRight];
[graphicsContext restoreGraphicsState];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment