Cocoa osx NSTableview line highlight color

In my application, I have a NSTableView view with a single column. The line highlight color is set to normal (blue). I need to change this color to my own color. In the designer of the interface, I tried to change it, but the only parameters are "No, normal and source list".

I tried this solution without success: stack overflow

I read that I need to use this delegate method, but I do not know how to use it.

- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row 

I tried drawing a string in this method, but I get invalid context warnings and the string still holds with the same bright color. Please write a simple example using this delegate method:

Need help please. Thanks in advance.

+6
source share
4 answers

From this link .

MyNSTableRowView.h

 #import <Cocoa/Cocoa.h> @interface MyNSTableRowView : NSTableRowView @end 

MyNSTableRowView.m

 #import "MyNSTableRowView.h" @implementation MyNSTableRowView - (id)init { if (!(self = [super init])) return nil; return self; } - (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]; } } @end 

AppDelegate.m

 - (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row { MyNSTableRowView *rowView = [[MyNSTableRowView alloc]init]; return rowView; } 
+8
source

Here is the answer of user 3065901 in Swift 3:

 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() } 
+7
source

If you are using a cell-based table view, set the NSTableView selectionHighLightStyle to None NSTableView and override the drawRow example below

 - (void)drawRow:(NSInteger)row clipRect:(NSRect)clipRect { NSColor* bgColor = Nil; // Set the color only when its first responder and key window if (self == [[self window] firstResponder] && [[self window] isMainWindow] && [[self window] isKeyWindow]) { bgColor = [NSColor brownColor]; } else { bgColor = [NSColor windowBackgroundColor];; } NSIndexSet* selectedRowIndexes = [self selectedRowIndexes]; if ([selectedRowIndexes containsIndex:row]) { [bgColor setFill]; NSRectFill([self rectOfRow:row]); } [super drawRow:row clipRect:clipRect]; } 
+2
source

Add the line below to the tableview method

ObjectiveC

 [yourtableview setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleNone]; 

Swift

 yourtableview.selectionHighlightStyle = .none 
0
source

Source: https://habr.com/ru/post/982340/


All Articles