/ / Ako prispôsobiť hlavičku NSTableView v systéme MacOS 10.12+? - maky, kakao, nstableview, swift3, nstableheadercell

Ako upraviť hlavičku NSTableView na macOS 10.12+? - mako, kakao, nstableview, swift3, nstableheadercell

MacOS 10.12+, Xcode 8+, Swift 3:

Rád by som programovo prispôsobil písmo a kresbu hlavičky NSTableView. Viem o tom, že sú na to staršie otázky, ale nemohol som nájsť nič, čo by dnes fungovalo.

Napríklad som sa pokúsil podtriedu NSTableHeaderCell nastaviť vlastné písmo:

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)
}
}

A potom použite túto podtriedu v mojom zobrazení tabuľky:

tableColumn.headerCell = MyHeaderCell()

Na konzole sa zobrazuje správa „MyHeaderCell sa kreslí“, ale písmo záhlavia tabuľky sa nezmení.

odpovede:

2 pre odpoveď č. 1

Vďaka komentárom od @HeinrichGiesen a@ Willeke, mám to funkčné. Uverejňujem ho tu pre prípad, že by to niekomu pomohlo. Všimnite si, že môj spôsob prispôsobenia farby pozadia nie je tak flexibilný. Som naozaj len tónovanie predvolené výkresu. Je to dosť dobré pre moje účely.

final class MyHeaderCell: NSTableHeaderCell {

// Customize background tint for header cell
override func draw(withFrame cellFrame: NSRect, in controlView: NSView) {
super.draw(withFrame: cellFrame, in: controlView)
NSColor(red: 0.9, green: 0.9, blue: 0.8, alpha: 0.2).set()
NSRectFillUsingOperation(cellFrame, .sourceOver)
}

// Customize text style/positioning for header cell
override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) {
attributedStringValue = NSAttributedString(string: stringValue, attributes: [
NSFontAttributeName: NSFont.systemFont(ofSize: 11, weight: NSFontWeightSemibold),
NSForegroundColorAttributeName: NSColor(white: 0.4, alpha: 1),
])
let offsetFrame = NSOffsetRect(drawingRect(forBounds: cellFrame), 4, 0)
super.drawInterior(withFrame: offsetFrame, in: controlView)
}
}