Could anybody suggest a method to select all the text of an NSTextField
when the user clicks it?
I did find suggestions to subclass NSTextField
I like Konstantin's solution, but that one will select on every mouse down. Here's a variation I'm using to select in mouseDown
method, but only if it just became the first responder:
class SelectingTextField: NSTextField {
var wantsSelectAll = false
override func becomeFirstResponder() -> Bool {
wantsSelectAll = true
return super.becomeFirstResponder()
}
override func mouseDown(with event: NSEvent) {
super.mouseDown(with: event)
if wantsSelectAll {
selectText(self)
wantsSelectAll = false
}
}
}