Vladimir Prudnikov
Vladimir Prudnikov

Reputation: 7242

Center placeholder string in NSSearchField after it was changed

I have a NSSearchField in UI with a placeholderString. By default its value is Search and it is centered automaticaly. But when user change selection in the sidebar I replace placeholder string with "Search in {NAME OF SIDEBARITEM}". For example:

enter image description here

As you can see placeholder string is not centered anymore. It will be centered after this search field activated and then deactivated.

Ho do I force NSSearchField to reposition placeholder string after it was changed?

Upvotes: 3

Views: 517

Answers (3)

Vladimir Prudnikov
Vladimir Prudnikov

Reputation: 7242

The only working solution I fond is to set search field's stringValue to some non-empty value and then to previously set value like so right after placeholder was changes:

@IBAction func setPlaceholder(sender: AnyObject) {
    self.searchField.placeholderString = self.inputField.stringValue
    let currentSearchStringValue = self.searchField.stringValue
    self.searchField.stringValue = "-"
    self.searchField.stringValue = currentSearchStringValue
}

Upvotes: 0

jrc
jrc

Reputation: 21939

Call [searchField setNeedsLayout:YES] after setting the placeholder string.

Upvotes: 1

Pol
Pol

Reputation: 4028

The workaround I'm currently using is to add some whitespace padding to all placeholder strings so they are roughly the same size.

Upvotes: 0

Related Questions