Change track selection range for NSTextField (cocoa)

Does anyone know how I can track changes in values NSTextField.currentEditor.selectedRange

for NSTextField

?

There is this wonderful thing NSTextViewDidChangeSelectionNotification

, it does exactly what I need, but only works for NSTextView

.

I tried to play with KVC / KVO but I didn't get what I wanted. I am guessing that 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 the values ​​from the NSTextField.currentEditor.selectedRange

text above. In real time, i.e. I want to constantly update the content of my label with selection length and starting position from NSTextField.currentEditor.selectedRange

when selecting a region of text.

+3


source to share


2 answers


NSTextField

uses the current window's margin editor to actually edit the text, and this NSTextView

. To subscribe to NSTextViewDidChangeSelectionNotification

this text view, you need to figure out when your field is receiving keyboard focus and then request it currentEditor

.

Unfortunately it controlTextDidBeginEditing:

never seems to be called, but you can override becomeFirstResponder

to get the same effect (be sure to call super).



I can't seem to find a good place to unsubscribe as the textbox only has keyboard focus for a second and then loses it when the NSTextView field editor is created and activated.

So in the end what I am doing is subscribing to a notification with an object nil

when the view is created, unsubscribing in the dealloc and checking to see if notification.object == self.currentEditor

the notification handler is present and ignoring all others.

+1


source


You can implement -windowWillReturnFieldEditor:toObject:

in your dedet NSWindow

and return a different field editor for the controls you need, perhaps with appropriate notifications NSTextView

configured just for that field editor. Or, of course, you can return your own subclass NSTextView

, although this is probably not needed here.



+1


source







All Articles