I'm doing some custom drawing in a NSSearchFieldCell
subclass. However overriding any of its two drawing methods causes the placeholder text not aligned.
For example, just by using this custom NSSearchFieldCell
subclass that overrides NSCell
's drawing method would cause the placeholder text to be left-aligned.
class CustomSearchFieldCell: NSSearchFieldCell {
override func draw(withFrame cellFrame: NSRect, in controlView: NSView) {
super.draw(withFrame: cellFrame, in: controlView)
}
override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) {
super.drawInterior(withFrame: cellFrame, in: controlView)
}
}
This is even after setting the search field's centersPlaceholder
property to true
and both re-layout the search field or reset the search field's stringValue
.
Commenting out those two methods would make the placeholder text (and magnifying glass back centered again.
However, just one override (even though it does nothing and only calls its superclass' implementation) would make the search field's placeholder text and magnifying glass become left-aligned.
The question is, how to get thecenter align placeholder
work and still have the custom drawing?
Note that I need to do some custom drawing and mouse handling within the cell, hence the overrides are required.
This was observed on macOS 10.12.6.
You should look into this method because it gets called when mouse has left the text field and update the frame accordingly :-
I had a requirement to vertically centre the text of NSSearchFieldCell and below code did the trick so you can give it a try to centre the components you want - :
I have made it in objective-c so read it accordingly and make it useful in swift.