how to change NSOutlineView or NSTableView column header font?

is it really impossible to do this?


I tried changing it form the xib designer view and it just reverted back to System font size 11


so I tried overriding the header cell, and put my custom font directly to the


class MyTableHeaderCell: NSTableHeaderCell {

let customFont: NSFont = NSFont.systemFont(ofSize: 13) // setting the custom font to use on the header cell

override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) {

var mutable = NSMutableAttributedString(attributedString: attributedStringValue)

mutable.removeAttribute(.font, range: NSRange(location: 0, length: mutable.length))

mutable.addAttribute(.font, value: customFont, range: NSRange(location: 0, length: mutable.length))


attributedStringValue = mutable

super.drawInterior(withFrame: cellFrame, in: controlView)

}

}


this would work most of the time, but sometimes when I move/resize the columns around I would get a crash due to the font's _sharedFontInstanceInfo being nil. _sharedFontInstanceInfo is a private property of NSFont


I also get some logs such as:

2018-10-18 18:02:01.579736-0700 NSOutlineView[96917:38014178] NSFont instance 0x6040000a9190 is over-released.

2018-10-18 18:04:36.420963-0700 NSOutlineView[97287:38028691] A system font, ".AppleSystemUIFont 13.00 pt. P [] (0x6040000632d0) fobj=0x61400000c240, spc=3.59", is requested to be deallocated. Ignoring...


I'd like to know 2 things I suppose,


1. is there a proper supported way of doing this? I imagine there is because Finder.app table header fonts is different than the default one that NSOutlineView comes with

2. why does the crash happen? what does those debug logs mean? what can I do if it's NSFont's internal properties that's busted? did I use the API incorrectly?


thanks!

Replies

AFAIK, this does not require to define a subclass and override the drawing method.


In Objective-C, for instance in the -(void)awakeFromNib method of your controller:


for(NSTableColumn * tTableColumn in myTableView.tableColumns)
  ((NSTableHeaderCell *)tTableColumn.headerCell).font=[NSFont systemFontOfSize:13.0];

Hi packagesdev,


I've tried that before, unfortunately that doesnt work. It always reverts to the standard system font size 11 during rendering, hence why I subclassed in the first place