Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save zpasternack/b8ae67901206bc6f80c9b0c65df8a8a8 to your computer and use it in GitHub Desktop.

Select an option

Save zpasternack/b8ae67901206bc6f80c9b0c65df8a8a8 to your computer and use it in GitHub Desktop.

Revisions

  1. zpasternack created this gist Mar 8, 2017.
    22 changes: 22 additions & 0 deletions VerticallyCenteredTextFieldCell.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    // 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)
    }

    }