// Makes the text in an NSTextFieldCell vertically centered. Works with single line, non-editable cells. // Maybe doesn't work with others. // Stolen from this stackoverflow question: // http://stackoverflow.com/questions/1235219/is-there-a-right-way-to-have-nstextfieldcell-draw-vertically-centered-text import Cocoa class VerticallyCenteredTextFieldCell: NSTextFieldCell { override func titleRect(forBounds rect: NSRect) -> NSRect { var titleFrame = super.titleRect(forBounds: rect) let titleSize = self.attributedStringValue.size() titleFrame.origin.y = rect.origin.y + (rect.size.height - titleSize.height) / 2.0 return titleFrame } override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) { let titleRect = self.titleRect(forBounds: cellFrame) self.attributedStringValue.draw(in: titleRect) } }