Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change selection color on view-based NSTableView

Standard highlighting color in OS X applications is blue.

Is it possible to change it to another color, e.g. gray?

Note that I am using the new view-based NSTableView available starting from OS X 10.7.

like image 979
Dev Avatar asked Feb 27 '12 11:02

Dev


2 Answers

Since you're using the view based NSTableView, you can subclass NSTableRowView, feed it to the table delegate method - (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row;, then customize your selection in the row view class.

Here's an example:

- (void)drawSelectionInRect:(NSRect)dirtyRect {     if (self.selectionHighlightStyle != NSTableViewSelectionHighlightStyleNone) {         NSRect selectionRect = NSInsetRect(self.bounds, 2.5, 2.5);         [[NSColor colorWithCalibratedWhite:.65 alpha:1.0] setStroke];         [[NSColor colorWithCalibratedWhite:.82 alpha:1.0] setFill];         NSBezierPath *selectionPath = [NSBezierPath bezierPathWithRoundedRect:selectionRect xRadius:6 yRadius:6];         [selectionPath fill];         [selectionPath stroke];     } } 
like image 195
James Chen Avatar answered Oct 03 '22 01:10

James Chen


Here is James Chen's solution in Swift 3. I've also added the delegate method.

class MyNSTableRowView: NSTableRowView {      override func drawSelection(in dirtyRect: NSRect) {         if self.selectionHighlightStyle != .none {             let selectionRect = NSInsetRect(self.bounds, 2.5, 2.5)             NSColor(calibratedWhite: 0.65, alpha: 1).setStroke()             NSColor(calibratedWhite: 0.82, alpha: 1).setFill()             let selectionPath = NSBezierPath.init(roundedRect: selectionRect, xRadius: 6, yRadius: 6)             selectionPath.fill()             selectionPath.stroke()         }     } } 

NSTableViewDelegate:

func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {     return MyNSTableRowView() } 
like image 30
Felix Avatar answered Oct 02 '22 23:10

Felix