Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I override layout of NSTableHeaderView?

I am trying to produce a custom header for my NSTableView. I would like to change the font of the header text and remove the borders and vertical separators.

My current top and bottom header is shown below:

enter image description here

Does anyone know how do I do this please?

UPDATE: After applying the fix the header now looks as I intended:

enter image description here

like image 964
iphaaw Avatar asked Dec 19 '22 00:12

iphaaw


2 Answers

Actually both the answers from mprudhom and Pronto contributed nicely to the final fix. Many thanks to both of you.

I thought I'd post my final answer so anyone following on can see how I fixed it.

I couldn't use mprudhom's code alone as my table only had a NSTableHeaderView and no TableHeaderCells. Fortunately Pronto came to the rescue on that:

class MyTable: NSTableView, NSTableViewDataSource, NSTableViewDelegate
{
    override func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView?
    {
        tableColumn!.headerCell = MyTableHeaderCell(textCell: tableColumn!.identifier)
    }
}

I used the NSTableHeaderCell code directly from mprudhom:

final class MyTableHeaderCell : NSTableHeaderCell
{
    required init?(coder aDecoder: NSCoder)
    {
        fatalError("init(coder:) has not been implemented")
    }

    override init(textCell: String)
    {
        super.init(textCell: textCell)
        self.font = NSFont.boldSystemFontOfSize(14)
    }

    override func drawWithFrame(cellFrame: NSRect, inView controlView: NSView)
    {
        //super.drawWithFrame(cellFrame, inView: controlView)//, since that is what draws borders
        self.drawInteriorWithFrame(cellFrame, inView: controlView)
    }

    override func drawInteriorWithFrame(cellFrame: NSRect, inView controlView: NSView)
    {
        let titleRect = self.titleRectForBounds(cellFrame)
        self.attributedStringValue.drawInRect(titleRect)
    }
}

Once these two methods were implemented, I had one last problem in that the NSTableHeaderView was drawn without the border as required but did have a grey background. So I overrode the NSTableHeaderView class with this method:

class ForecastHeaderView : NSTableHeaderView
{
    required init?(coder: NSCoder)
    {
        super.init(coder: coder)
    }

    override init(frame frameRect: NSRect) 
    {
        super.init(frame: frameRect)
        self.wantsLayer = true
    }

    override func drawLayer(layer: CALayer, inContext ctx: CGContext)
    {
        super.drawLayer(layer, inContext: ctx)
        layer.backgroundColor = CGColorCreateGenericGray(1.0, 1.0)
    }
}
like image 158
iphaaw Avatar answered Mar 21 '23 01:03

iphaaw


One way to do it, in your NSTableViewDelegate's viewForTableColumn -function:

if tableColumn!.identifier == "description" {
    let cell = NSTableHeaderCell()
    cell.title = "New Title"
    cell.backgroundColor = NSColor.redColor()
    cell.drawsBackground = true
    cell.bordered = false
    cell.font = NSFont(name: "Helvetica", size: 16)
    tableColumn!.headerCell = cell
}
like image 38
Prontto Avatar answered Mar 21 '23 00:03

Prontto