Reputation: 9576
I have a NSTextField
that uses no border and window background color while it is displayed but I want it to change to have the default border and white BG color when being edited. I know I can change these properties with:
nameTextField.bezeled = true
nameTextField.backgroundColor = NSColor.textBackgroundColor()
What I don't know is how to be notified when the textfield is started to be edited and when it's ended. There seem to be no actions for this. Is there any other way how could this behaviour be achieved?
EDIT: Actually, edit end can be detected via the textfield's change action when setting Action to "Send on End Editing" so that's solved but still how do I detect start editing?
Upvotes: 3
Views: 2930
Reputation: 10479
You can set the delegate
of NSTextField
:
nameTextField.delegate = self
then you can set a different state:
func control(control: NSControl, textShouldBeginEditing fieldEditor: NSText) -> Bool {
nameTextField.bezeled = true
nameTextField.backgroundColor = NSColor.textBackgroundColor()
return true
}
func control(control: NSControl, textShouldEndEditing fieldEditor: NSText) -> Bool {
nameTextField.bezeled = false
nameTextField.backgroundColor = NSColor.windowBackgroundColor()
return true
}
EDIT:
I think you can subclass NSTextField
and override the becomeFirstResponder
and the resignFirstResponder
, then you know the NSTextField
has the focus or not.
Upvotes: 3