Does anyone have any idea how I can track NSTextField.currentEditor.selectedRange
value changes for NSTextField
?
There is this wonderful thing NSTextViewDidChangeSelectionNotification
, it does exactly what I need, but it works only for NSTextView
.
I've tried to play with KVC/KVO but I didn't get what I wanted. I assume I did something wrong.
I will try to explain what I need to achieve.
I have NSTextField
, below I have a label where I want to put values from NSTextField.currentEditor.selectedRange
of text selection above. In realtime, i.e. I want to update my label content continuously with selection length and start position from NSTextField.currentEditor.selectedRange
while selecting area of text.
As mentioned in this "retired" article, the "field editor" designates the current text field as its delegate. Since the field editor is an NSTextView
instance, we can implement any NSTextViewDelegate
method in our NSTextField
subclass.
- (void)textViewDidChangeSelection:(NSNotification *)notification
{
NSRange selection = self.currentEditor.selectedRange;
NSLog(@"selection = (location: %lu, length: %lu)", selection.location, selection.length);
}
Enjoy! ✌️
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With