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

查看:45
本文介绍了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 的文本视图,并将其设置为第一响应者.

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不是程序员.我称它为孩子)

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)
    }

}

对不起,很快.但我让它在我自己的应用程序中工作.不过不得不修改.应该可以.

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天全站免登陆