My OS X app has a NSTableView
with larger row height:
myTableView.rowSizeStyle = .large
I try to put some text in it but I found that the text I put in cannot be vertically centered.
Please see the code below. I have three columns and the first two use NSTextField
and NSTextView
to put some text. The third one is a check box button.
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
if (tableColumn?.identifier == "Column1") {
let field = NSTextField()
field.stringValue = "someText"
field.isBordered = false
field.isEditable = false
field.backgroundColor = NSColor.red
return field
} else if (tableColumn?.identifier == "Column2") {
let field = NSTextView()
field.string = "someText"
field.backgroundColor = NSColor.green
return field
} else if (tableColumn?.identifier == "Column3") {
let field = NSButton()
field.setButtonType(NSSwitchButton)
field.title = "check me"
return field
}
return nil
}
My code is pretty much based on Apple's document: "Creating and Configuring an NSTextField Cell".
Here is the result:
As you can see, the texts in the first two columns are not vertically centered while the check box is vertically centered automatically.
When using func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView?
just like the Apple document has shown, what is the right way to put text so that it can be vertically centered?
EDIT: For some reason, I don't use Interface Builder. So the question sticks to coding the UI programatically.