NSTextField: exposing it to copy and paste methods

I am trying to access the copy, cut and paste methods of an NSTextField instance in my window delegate in order to customize these methods. I find that unlike tableViews and textViews, the copy, paste, and cut operations of a text field do not respond to the delegate. I understand that all text controls share the window's field editor, but that doesn't seem to be the case.

I thought that perhaps the TextField field editor was not shared with the window delegate, however I did some testing, I see that when I type, these field editor is identical - very strange.

My current job uses an instance of a subclass of NSTextView, where the copy and paste action methods respond as needed. This, however, has its problems, and I was hoping there was some way to get NSTextFields to work as expected.

+2
source share
2 answers

The nstext field does not have copy and paste functions. They are only in nstextview. The catch is that when editing a text field, it opens a text field called a field commentator during editing, and sets this as the first responder.

How to solve:

each text attached to it has a cell as a child. (ps is not a programmer, I call it a child)

enter image description here

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

excuse him quickly. but I work in my application. However, it was necessary to modify. Must work.

credit:

How to disable right-click context menus in NSTextField (Cocoa)?

and Ken Thomases who pointed out the field editor

+2
source

Perhaps you can take a look at NSTextField:

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

This will allow you to intercept the call by setting up the answer.

0
source

Source: https://habr.com/ru/post/1493667/


All Articles