NSTextField in NSTableCellView

前端 未结 4 1970
面向向阳花
面向向阳花 2021-01-01 03:20

I have a view based NSTableView with a custom NSTableCellView. This custom NSTableCellView has several labels (NSTextField). The whole UI of the NSTableCellView is built in

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-01 04:13

    Override setBackgroundStyle: on the NSTableCellView to know when the background changes which is what affects what text color you should use in your cell.

    For instance:

    - (void)setBackgroundStyle:(NSBackgroundStyle)style
    {
        [super setBackgroundStyle:style];
    
        // If the cell's 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 source list table views the cell view's background style is set to Light, as is its textField's backgroundStyle, however the textField also draws a shadow under its text and haven't yet found exactly what is controlling that / determining that should it happen.

提交回复
热议问题