/ / Jak dostosować nagłówek NSTableView na macOS 10.12+? - macos, kakao, nstableview, swift3, nstableheadercell

Jak dostosować nagłówek NSTableView w systemie MacOS 10.12 lub nowszym? - maca, kakao, nstableview, swift3, nstableheadercell

MacOS 10.12+, Xcode 8+, Swift 3:

Chciałbym programowo dostosować czcionkę i rysunek nagłówka NSTableView. Wiem, że są na to starsze pytania, ale nie mogłem znaleźć niczego, co działa dzisiaj.

Na przykład próbowałem podklasować NSTableHeaderCell, aby ustawić niestandardową czcionkę:

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 następnie użyj tej podklasy w moim widoku tabeli:

tableColumn.headerCell = MyHeaderCell()

Widzę komunikat „MyHeaderCell rysuje” w konsoli, ale czcionka nagłówka tabeli się nie zmienia.

Odpowiedzi:

2 dla odpowiedzi № 1

Dzięki komentarzom z @HeinrichGiesen i@Willeke, to działa. Umieszczam go tutaj, na wypadek, gdyby to pomogło komuś w dół. Zauważ, że mój sposób dostosowywania koloru tła nie jest tak elastyczny. Naprawdę tylko zabarwiam domyślny rysunek. Jest wystarczająco dobry dla moich celów.

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