NSTextField: exposing its Copy and Paste methods

折月煮酒 提交于 2019-11-28 14:16:20
jiminybob99

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.

How to solve:

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

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

class cell: NSTextFieldCell {

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

this function allows you to insert your own custom 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.

credit to:

How to disable context menus with right mouse click in an NSTextField (Cocoa)?

and Ken Thomases who pointed out the field editor

Maybe you could take a look at NSTextField's:

- (BOOL)writeSelectionToPasteboard:(NSPasteboard *)pboard type:(NSString *)type;
- (BOOL)readSelectionFromPasteboard:(NSPasteboard *)pboard type:(NSString *)type;

This would allow you to intercept the call customize the response.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!