简体   繁体   English

NSTextField(可可)的跟踪选择范围更改

[英]Track selection range change for NSTextField (cocoa)

Does anyone have any idea how I can track NSTextField.currentEditor.selectedRange value changes for NSTextField ? 没有人有任何想法,我怎么可以跟踪NSTextField.currentEditor.selectedRange为价值变动NSTextField

There is this wonderful thing NSTextViewDidChangeSelectionNotification , it does exactly what I need, but it works only for NSTextView . 有这个奇妙的东西NSTextViewDidChangeSelectionNotification ,它完全符合我的需要,但它只适用于NSTextView

I've tried to play with KVC/KVO but I didn't get what I wanted. 我试过和KVC / KVO玩,但我得不到我想要的东西。 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. 我有NSTextField ,下面我有一个标签,我想在上面的文本选择的NSTextField.currentEditor.selectedRange中放置值。 In realtime, ie I want to update my label content continuously with selection length and start position from NSTextField.currentEditor.selectedRange while selecting area of text. 实时,即我想在选择文本区域时,通过选择长度和NSTextField.currentEditor.selectedRange起始位置连续更新我的标签内容。

NSTextField uses the field editor of the current window to do the actual text editing, and that is an NSTextView . NSTextField使用当前窗口的字段编辑器进行实际的文本编辑,这是一个NSTextView To subscribe to NSTextViewDidChangeSelectionNotification on that text view , you need to find out when your field gets keyboard focus and then ask it for its currentEditor . 要在该文本视图上订阅NSTextViewDidChangeSelectionNotification ,您需要找出您的字段何时获得键盘焦点,然后询问它的currentEditor

Sadly, it seems controlTextDidBeginEditing: is never called, but you can override becomeFirstResponder to get the same effect (be sure to call super). 可悲的是,似乎controlTextDidBeginEditing:永远不会被调用,但你可以覆盖becomeFirstResponder以获得相同的效果(一定要调用super)。

I can't find a good spot to unsubscribe though, as the text field only has keyboard focus for a split-second, and then loses it when it creates and activates the field editor NSTextView. 我找不到取消订阅的好地方,因为文本字段只有一秒钟的键盘焦点,然后在创建和激活字段编辑器NSTextView时丢失它。

So in the end, what I'm doing is subscribing for the notification with a nil object when the view is created, unsubscribing in dealloc, and checking if notification.object == self.currentEditor in the notification handler and ignoring all others. 所以最后,我正在做的是在创建视图时使用nil对象订阅通知,在dealloc中取消订阅,并在通知处理程序中检查notification.object == self.currentEditor是否忽略所有其他通知。

You could implement -windowWillReturnFieldEditor:toObject: on your NSWindow delegate and return a different field editor for the control(s) you care about, perhaps with the relevant NSTextView notifications set up just for that field editor. 您可以在NSWindow委托上实现-windowWillReturnFieldEditor:toObject:并为您关注的控件返回不同的字段编辑器,可能只为该字段编辑器设置相关的NSTextView通知。 Or, of course, you could return your own subclass of NSTextView , though that is probably unnecessary here. 或者,当然,您可以返回自己的NSTextView子类,尽管这可能不必要。

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. 由于字段编辑器是NSTextView实例,我们可以在NSTextField子类中实现任何NSTextViewDelegate方法。

- (void)textViewDidChangeSelection:(NSNotification *)notification
{
  NSRange selection = self.currentEditor.selectedRange;
  NSLog(@"selection = (location: %lu, length: %lu)", selection.location, selection.length);
}

Enjoy! 请享用! ✌️ ✌️

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM