Handling Drops in a Text Field

To handle drag and drop in a text field, you need to subclass NSTextField and add support for the operation. This works well as long as the text field is not currently being edited. To handle drag and drop while the text field is being edited, you must implement support in the field editor.

To provide a custom field editor for your text field (or any other control) you need to implement a method to respond to the NSWindow delegate message windowWillReturnFieldEditor:toObject: in the delegate of the window containing the text field you want to respond to drags. The client specified in the toObject: argument is the text field that is about to be edited, for which it uses the NSTextView object you return instead of the standard field editor.

NSTextView has support for drag and drop through the NSDragging category. However, an NSTextView object registers for draggable pasteboard types only if it is set up to handle rich text (see the setRichText: method) and allows attached files (see the setImportsGraphics: method). By default, NSTextView does not accept dragged files.

To support new data types for dragging operations, you should override the acceptableDragTypes method. Your implementation of these methods should invoke the superclass implementation, add the new data types to the array returned from the superclass, and return the modified array. You must also override the appropriate methods of the NSDraggingDestination protocol to support importing those types. See that protocol reference for more information. Also see Drag and Drop Programming Topics.