Search code examples
objective-cxcodecocoansoutlineview

How to set text color of top-level NSOutlineView item


I use the same NSTableCellView for both top-level and other items. I set the text color the same way for all items:

- (nullable NSView*) outlineView:(NSOutlineView *)outlineView viewForTableColumn:(nullable NSTableColumn *)tableColumn item:(id)item_;
{
    NSTableCellView *cell = [outlineView makeViewWithIdentifier:@"DataCell" owner:self];
    cell.textField.stringValue = @"hi";
    cell.textField.textColor = NSColor.redColor;
    return cell;
}

Which results in top-level items having some kinda system color - not red.

enter image description here

My remaining relevant code:

- (BOOL) outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item_;
{
    return YES // only for the first item;
}

I've tried to subclass NSTableHeaderCell as advised in other topics but it's not working.

This is how I try to change the headerCells:

- (void) viewDidLoad;
{
    for (NSTableColumn *col in self.ov_sidebar.tableColumns) {
        col.headerCell = [NCTableHeaderCell_customizable new];
    }

    self.ov_sidebar.delegate = self;
    self.ov_sidebar.dataSource = self;
}

But none of the NCTableHeaderCell_customizable overiden methods are called. So I guess the custom headerCells must be set some other way. Likely it's not even the proper way to change to text color.

Do you have any clue how to properly set text color of top-level NSOutlineView items?

One possible lead, when I change the "Highlight" from "Source List" to "None" then it shows the list like this:

enter image description here

It has proper font color but unwanted formatting with disclosure and background with border.

So, shall I continue this way and try to fix this formatting as needed or is there a solution with "Source List"?


Solution

  • You are almost there. You May try this.

    for (NSTableColumn *col in self.ov_sidebar.tableColumns) {
        col.headerCell = [[MyTableHeaderCell alloc]initTextCell:@"hi"];
    }
    

    There are always some tradeoffs even you directly subclass from NSTableHeaderCell.

      @interface MyTableHeaderCell : NSTableHeaderCell
    
      @end
      @implementation MyTableHeaderCell
    
      -(NSColor *) textColor{
       return NSColor.blueColor;
      }
    
     @end
    

    This works AND is not recommended as they are not mentioned in documentation of NSTableHeaderCell.

    I show a simple way to achieve your goal.

    In IB add an extra TextField. and Tag it 1001 or whatever you like. your first one is textField of cellView and remember to hide it in IB. right! hide it but not remove it.

    Then change code to refer to the second textfield.

    NSTableCellView *cell = [outlineView makeViewWithIdentifier:@"DataCell" owner:self];
    NSTextField * field = (NSTextField * )[cell viewWithTag:1001];
    field.stringValue = ((MyData *) item_).value;
    field.textColor = NSColor.redColor;
    
    return cell;
    

    enter image description here

    enter image description here