2
votes

I have an OS X application written in Swift (thanks to Mathias and Gallagher) that uses a cell-based NSTableView. One of the requirements by the client was to be able to increase the font size of the text displayed in each text field cell. It all seemed pretty straight forward after a bit of stack overflow googling: set each NSTableView column’s dataCell font to the desired font and size; then subclass the NSTextFieldCell and override the drawInteriorWithFrame and titleRectForBounds and adjust the height to fit the rect.

However, since Apple has depreciated cell-based NSTableViews in favor of view-based I figured I should change my code to view-based.

Argh! What seemed like such a simple change has caused me two days of hair pulling grief. I can get the text font size to change just fine but the NSTextFieldCell NSRect height stays fixed. What few examples I’ve seen on the web are for iOS and don’t work for OS X.

Is there an easy way to do this?

1
I don't know about easy. But the general way to do this would be to calculate the height of the text with the font size and then use that height for the height of the text field.rocky
What makes you think that you have to use the view-based NSTableView to use a custom font size? You don't have to subclass anything for such a simple task.El Tomato
You can use delegation for changing the font sizeblackirishman
Do you need to be able to use a different font size for each separate cell (cells having different sizes from each other)? Or just be able to change the size used by all of the cells, together?Ken Thomases
I am calculating the height of the text for with NSFont.systemFontOfSize and this correctly sets the height of the table view rows, but not the text itself.WholeCheese

1 Answers

1
votes

I marked your question as favorite, tried it, failed, and slept on it for a long time. But I think I've solved it for all future Googlers.


You shouldn't have to subclass anything for this. Try this:

In Interface Builder:

  1. Select the Table View Cell (#1 in the screenshot), open the Identity Inspector (Cmd + Opt + 3) and set its Identifier to myTableViewCell
  2. Select the Text Cell (#2 in the screenshot), open the File Inspector (Cmd + Opt + 1) and uncheck Use Auto Layout

enter image description here

In your view controller:

Connect the outlets and actions and you should be fine:

class AppDelegate: NSObject, NSApplicationDelegate, NSTableViewDataSource, NSTableViewDelegate  {

    @IBOutlet weak var window: NSWindow!
    @IBOutlet weak var tableView: NSTableView!

    var names = ["Luke Skywalker", "Han Solo", "Chewbecca"]
    var fontSize = NSFont.systemFontSize()

    func applicationDidFinishLaunching(aNotification: NSNotification) {
        self.tableView.setDataSource(self)
        self.tableView.setDelegate(self)
    }

    func numberOfRowsInTableView(tableView: NSTableView) -> Int {
        return self.names.count
    }

    func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? {
        let cellView = tableView.makeViewWithIdentifier("myTableViewCell", owner: self) as! NSTableCellView
        let textField = cellView.textField!
        let fontDescriptor = textField.font!.fontDescriptor

        textField.font = NSFont(descriptor: fontDescriptor, size: self.fontSize)
        textField.stringValue = self.names[row]
        textField.sizeToFit()
        textField.setFrameOrigin(NSZeroPoint)

        tableView.rowHeight = textField.frame.height + 2

        return cellView
    }

    @IBAction func makeBigger(sender: AnyObject) {
        self.fontSize += 1
        self.tableView.reloadData()
    }

    @IBAction func makeSmaller(sender: AnyObject) {
        self.fontSize -= 1
        self.tableView.reloadData()
    }
}