MacOS 10.12+, Xcode 8+, Swift 3:
I'd like to programmatically customize the font and drawing of an NSTableView header. I know there are older questions about this, but I couldn't find anything that works today.
For example, I tried to subclass NSTableHeaderCell to set a custom font:
class MyHeaderCell: NSTableHeaderCell {
override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) {
NSLog("MyHeaderCell is drawing")
font = NSFont.boldSystemFont(ofSize: 12)
super.drawInterior(withFrame: cellFrame, in: controlView)
}
}
And then use that subclass in my table view:
tableColumn.headerCell = MyHeaderCell()
I see the message "MyHeaderCell is drawing" in the console, but the font of the table header doesn't change.
It has been some time since anyone has addressed this question. I've encountered the same very frustrating problem using Swift 5 and Xcode 12. Here is what I have learned using an nstableview approach that does not require subclassing.
at the beginning of the func tableView(_ myTable: NSTableView... add the following lines of code:
This code changes the headerCell background color (and fill1Tint is some NSColor).
Then add:
within each tableColumn.identifier block, add an attributed text string such as:
This code enables both a different background and centered, attributed text.