Search code examples
macoscocoamacos-sierranssearchfield

NSSearchField with left aligned placeholder


After setting centersPlaceholder = false, the placeholder aligns correctly at the left, but the search button doesn't show its image. The same is true for the cancel button when entering some text. Is that the correct behavior or am I missing something?


Solution

  • This related question sheds some light on how the strange behavior could be (ab)used to force a left-align in the field. Simply subclass NSSearchFieldCell and override draw method:

    import Cocoa
    
    class FilterFieldCell: NSSearchFieldCell {
        override func draw(withFrame cellFrame: NSRect, in controlView: NSView) {
            super.draw(withFrame: cellFrame, in: controlView)
        }
    }
    

    And then set your search field cell to this class.