如何检测双击NSTextField [英] how to detect double Click in NSTextField

查看:496
本文介绍了如何检测双击NSTextField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义NSTextField,我想检测用户在文本字段中的双击。我的目标:我想要能够双击表达式中的括号,例如(2 + 2)= 4,并选择匹配的括号内的一切。思考我可以这样做...

   - (void)textView:(NSTextView *)textView doubleClickedOnCell:(id< NSTextAttachmentCell>)cell inRect:(NSRect)cellFrame atIndex:(NSUInteger)charIndex; 

但它从未在我的自定义NSTextField中调用。



然后我想我可以覆盖-mouseDown,但是也没有得到调用。我被骗了。



谢谢!



Philip

解决方案

文本字段不处理编辑,因此。当文本字段具有焦点时,文本视图将添加到窗口,与文本字段的区域重叠。这称为字段编辑器它负责处理编辑。



似乎最可能的地方,你改变双击的行为是在文本存储对象使用的文本视图。 NSTextStorage 继承自 NSMutableAttributedString ,它继承自 NSAttributedString -doubleClickAtIndex:方法。该方法返回应通过双击特定索引选择的文本的范围。



因此,您需要实现 NSTextStorage 覆盖该方法,并在某些情况下返回不同的结果。 NSTextStorage 是类集群的半抽象基类。子类化它需要比平常多一点。你必须实现 NSAttributedString NSMutableAttributedString 的原始方法。请参见相关文档。 / p>

有几个地方可以通过用您的类的实例替换其文本存储对象来自定义字段编辑器:




  • 您可以实现 NSTextFieldCell 的自定义子类。设置文本字段以将其用作其单元格。在你的子类中,重载 -fieldEditorForView:。在你的覆盖中,实例化一个 NSTextView 。获取它的 layoutManager ,并调用 -replaceTextStorage:,传递它一个自定义文本存储类的实例。 (这比将文本编辑涉及的对象的层次结构放在一起更容易,虽然你可以自己做)。将文本视图的 fieldEditor 属性设置为true, 使用您的自定义文本存储创建,配置并返回 NSTextView ,如上所述。


I have a custom NSTextField and I'd like to detect double clicks by the user in the text field. My goal: I want to be able to double click on a parenthesis in an expression, such as "(2+2) = 4" and have it select everything inside the matching parentheses. Thought I could do this with...

- (void)textView:(NSTextView *)textView doubleClickedOnCell:(id <NSTextAttachmentCell>)cell inRect:(NSRect)cellFrame atIndex:(NSUInteger)charIndex;

but it never gets called in my custom NSTextField.

Then I thought I could override -mouseDown, but that isn't getting called either. I'm stumped. Any suggestions for what should be an easy function to implement.

Thanks!

Philip

解决方案

A text field does not handling editing, as such. When a text field has focus, a text view is added to the window, overlapping the area of the text field. This is called the "field editor" and it is responsible for handling editing.

It seems the most likely place for you to change the behavior of a double-click is in the text storage object used by that text view. NSTextStorage inherits from NSMutableAttributedString which inherits from NSAttributedString which has a -doubleClickAtIndex: method. That method returns the range of the text that should be selected by a double-click at a particular index.

So, you'll want to implement a subclass of NSTextStorage that overrides that method and returns a different result in some circumstances. NSTextStorage is a semi-abstract base class of a class cluster. Subclassing it requires a bit more than usual. You have to implement the primitive methods of NSAttributedString and NSMutableAttributedString. See the docs about it.

There are a few places to customize the field editor by replacing its text storage object with an instance of your class:

  • You could implement a custom subclass of NSTextFieldCell. Set your text field to use this as its cell. In your subclass, override -fieldEditorForView:. In your override, instantiate an NSTextView. Obtain its layoutManager and call -replaceTextStorage: on that, passing it an instance of your custom text storage class. (This is easier than putting together the hierarchy of objects that is involved with text editing, although you could do that yourself.) Set the fieldEditor property of the text view to true and return it.
  • In your window delegate, implement -windowWillReturnFieldEditor:toObject:. Create, configure, and return an NSTextView using your custom text storage, as above.

这篇关于如何检测双击NSTextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆