1

I have a custom NSTableCellView where the background of the cell has a colored bar that changes length from 0-100% based on the value of the text in the cell (it's part of a dynamic bar graph in the table). The problem is that depending on the length of the bar the text gets hidden as it is black text on a black bar.

enter image description here

The proper way to do this is to use a .exclusion blend mode when drawing the text. However that's something I've never done before and don't have anything doing the exact same type thing in my code base.

I've gotten this far with some custom highlight code used in rendering a PDF page but not even sure how to set up the context or do the actual draw as that was provided by PDFKit in that case.

class BarTableCellView: NSTableCellView {

    @IBOutlet var bar1: NSBox!
    @IBOutlet var bar1Width: NSLayoutConstraint!
    @IBOutlet var bar2: NSBox!
    @IBOutlet var bar2Width: NSLayoutConstraint!
    
    override func draw(_ dirtyRect: NSRect) {
        
        super.draw(dirtyRect)

        // Drawing code here.
        
        let context = NSGraphicsContext.current
        context?.saveGraphicsState()
     
        context.setBlendMode(CGBlendMode.exclusion)
    }   
} 
Jc Nolan
  • 450
  • 4
  • 15

0 Answers0