NSTextField Vertical alignment NSTextField Vertical alignment xcode xcode

NSTextField Vertical alignment


What you want is possible, but you'll have to make a subclass of NSTextFieldCell, and then use that subclass in your NSTextField. The key methods you want override are drawingRectForBounds:, selectWithFrame:, and editWithFrame:

Here is a blog post from the fantastic Daniel Jalkut about this, he even includes a downloadable version ready to go. The post is fairly old but it should still work fine.


For adjusting the text field to string size you have to calculate the text size with NSString's sizeWithFont: constrainedToSize: lineBreakMode: before and than adjust the text field's size with setting the frame.

For vertical alignment look at this question (and the answers).


Here is a Swift version of the solution linked to by sosborn above:

open class VerticallyCenteredTextFieldCell: NSTextFieldCell {        fileprivate var isEditingOrSelecting : Bool = false        open override func drawingRect(forBounds rect: NSRect) -> NSRect {        let rect = super.drawingRect(forBounds: rect)                if !isEditingOrSelecting {            let size = cellSize(forBounds: rect)            return NSRect(x: rect.minX, y: rect.minY + (rect.height - size.height) / 2, width: rect.width, height: size.height)        }                return rect    }        open override func select(withFrame rect: NSRect, in controlView: NSView, editor textObj: NSText, delegate: Any?, start selStart: Int, length selLength: Int) {        let aRect = self.drawingRect(forBounds: rect)        isEditingOrSelecting = true        super.select(withFrame: aRect, in: controlView, editor: textObj, delegate: delegate, start: selStart, length: selLength)        isEditingOrSelecting = false    }        open override func edit(withFrame rect: NSRect, in controlView: NSView, editor textObj: NSText, delegate: Any?, event: NSEvent?) {        let aRect = self.drawingRect(forBounds: rect)        isEditingOrSelecting = true        super.edit(withFrame: aRect, in: controlView, editor: textObj, delegate: delegate, event: event)        isEditingOrSelecting = false    }    }