NSTextField:暴露其复制和粘贴方法 [英] NSTextField: exposing its Copy and Paste methods

查看:3231
本文介绍了NSTextField:暴露其复制和粘贴方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在其窗口委托中访问NSTextField实例的复制,剪切和粘贴方法,因此我可以自定义这些方法。我发现,与tableViews和textViews不同,文本字段的复制,粘贴和剪切操作在代理中没有响应。我的理解是所有的文本控件共享窗口的字段编辑器,但似乎不是这样。

I am trying to access the copy, cut, and paste methods of a NSTextField instance in its window delegate so I can customize these methods. I find that unlike tableViews and textViews, the textfield's copy, paste and cut actions are not responsive in the delegate. My understanding is that all text controls share the window's field editor yet this does not seem to be the case.

我想也许TextField的字段编辑器没有被共享窗口委托,但是我做了一些测试,我看到,当我打字控制,这些字段编辑器是相同的 - 很奇怪。

I thought perhaps the TextField's field editor was not being shared with the window delegate, however I did some testing I see that as I am typing in control, those field editors are identical--very strange.

我目前的解决方法是使用NSTextView的子类实例,其中复制和粘贴操作方法根据需要进行响应。但是,这有自己的问题,我希望有一些方法可以让NSTextFields按预期工作。

My current work-around is to use a subclass instance of NSTextView where the copy and paste action methods respond as needed. This, however, has its own issues and I was hoping there was some way to get NSTextFields to work as expected.

推荐答案

nstextfield没有复制和粘贴功能。这些只在nstextview中找到。抓住的是,当编辑一个文本字段时,它在编辑期间打开一个称为fieldeditor的textview,并将其设置为第一个响应者。

A nstextfield does not have copy and paste functions. Those are only found in nstextview. the catch is that when a textfield is edited it opens up a textview called a fieldeditor during the editing and sets that as the first responder.

如何解决:

每个文本字段都有一个单元格作为子项连接。 (ps not programmer,Ill call it child)

each text filed has a cell as a child connected too it. (ps not programmer. Ill call it child)

此单元格有一种方法用于实现名为fieldeditorforview的自定义字段编辑器

The cell has a method for implementing a custom field editor called fieldeditorforview

class cell: NSTextFieldCell {

    override func fieldEditorForView(aControlView: NSView) -> NSTextView? {
        return ESPasteView()
    }
}

允许您插入自己的自定义nstextview

this function allows you to insert your own custom nstextview

这是我的自定义nstextview

here is my custom nstextview

class ESPasteView: NSTextView, NSTextViewDelegate {

    override func drawRect(dirtyRect: NSRect) {
        super.drawRect(dirtyRect)
    }

    override func paste(sender: AnyObject?) {
        Swift.print("user tries to paste")
        super.pasteAsPlainText(nil)
    }

}

对不起在swift。但我有它在我自己的应用程序工作。不得不修改。应该工作。

sorry its in swift. but i have it working in my own application. Had to modify though. Should work.

贷记到:

如何在NSTextField(Cocoa)中用鼠标右键禁用上下文菜单?

和指出字段编辑器的Ken Thomases

and Ken Thomases who pointed out the field editor

这篇关于NSTextField:暴露其复制和粘贴方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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