NSTextField in NSTableCellView

I have an NSTableView view with a custom NSTableCellView. This custom NSTableCellView has multiple labels (NSTextField). The entire user interface of NSTableCellView is built in IB.

NSTableCellView may be in a normal state and in a selected state. In the normal state, all text labels should be black; in the selected state, they should be white.

How can i do this?

+5
source share
4 answers

Probably the easiest way to achieve this would be to subclass NSTextField and override the drawRect: method in your subclass. There you can determine if an NSTableCellView instance containing your NSTextField instances is selected using this code (which I use with NSOutlineView, but it should also work with NSTableView):

BOOL selected = NO; id tableView = [[[self superview] superview] superview]; if ([tableView isKindOfClass:[NSTableView class]]) { NSInteger row = [tableView selectedRow]; if (row != -1) { id cellView = [tableView viewAtColumn:0 row:row makeIfNecessary:YES]; if ([cellView isEqualTo:[self superview]]) selected = YES; } } 

Then draw the view as follows:

 if (selected) { // set your color here // draw [self stringValue] here in [self bounds] } else { // call [super drawRect] } 
0
source

Replace setBackgroundStyle: in NSTableCellView to know when the background changes, which affects the color of the text that you should use in your cell.

For instance:

 - (void)setBackgroundStyle:(NSBackgroundStyle)style { [super setBackgroundStyle:style]; // If the cell text color is black, this sets it to white [((NSCell *)self.descriptionField.cell) setBackgroundStyle:style]; // Otherwise you need to change the color manually switch (style) { case NSBackgroundStyleLight: [self.descriptionField setTextColor:[NSColor colorWithCalibratedWhite:0.4 alpha:1.0]]; break; case NSBackgroundStyleDark: default: [self.descriptionField setTextColor:[NSColor colorWithCalibratedWhite:1.0 alpha:1.0]]; break; } } 

In the source table of the list, the style style of the background image of the cell is set to “Light”, as well as to its text element backgroundStyle, however textField also draws a shadow under its text and has not yet found exactly what controls this / definition, which should happen.

+14
source

This works no matter what style the table looks like:

 - (void)setBackgroundStyle:(NSBackgroundStyle)backgroundStyle { [super setBackgroundStyle:backgroundStyle]; NSTableView *tableView = self.enclosingScrollView.documentView; BOOL tableViewIsFirstResponder = [tableView isEqual:[self.window firstResponder]]; NSColor *color = nil; if(backgroundStyle == NSBackgroundStyleLight) { color = tableViewIsFirstResponder ? [NSColor lightGrayColor] : [NSColor darkGrayColor]; } else { color = [NSColor whiteColor]; } myTextField.textColor = color; } 
0
source

Swift 4

  override var backgroundStyle: NSView.BackgroundStyle { get { return super.backgroundStyle } set { self.yourCustomLabel.textColor = NSColor(calibratedWhite: 0.0, alpha: 1.0)//black } } 
0
source

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


All Articles