BalKrishan Yadav
BalKrishan Yadav

Reputation: 467

How to disable Dragging in UITextView in iOS 11

I am using UITextview in UITableview with copy and link detection for selection. Drag and drop is working without implementing Drag and drop feature on UITextview. So I want to disable UITextView drag and drop...

Upvotes: 10

Views: 5506

Answers (5)

JsW
JsW

Reputation: 1758

For those who want to disable the drop interaction, check out the following lines,


print(noteView.interactions) // you will find a drop interaction here
textView.interactions.removeAll(where: { $0 is UIDropInteraction })
print(noteView.interactions) // it's gone

likewise, both drag and drop interactions can be disabled/removed like that

textView.interactions.removeAll(where: { $0 is UIDropInteraction
                                      || $0 is UIDragInteraction })


Upvotes: 1

Hervé Kasparian
Hervé Kasparian

Reputation: 169

Hy, I tried a more simple solution, that seems to work :

if #available(iOS 11.0, *) {      
    myTextView.textDropDelegate=nil
}

So now, when I drop an Image in this area, I can handle it as an Image and not as the text of the path.

Upvotes: -1

grigorevp
grigorevp

Reputation: 701

I had a similar problem and have found a solution suitable for me.

So, I had a TableView with a single TextView inside every cell. I wanted to enable drag/drop for TableView in order to be able to move cells. But when I was long-pressing a cell, I was actually pressing its TextView, starting the drag session for it either. And whenever I tried to move that cell to a new position in TableView, the app was trying to insert the text from that cell's TextView to a new cell's TextView, performing a "drag and drop text copy operation".

In order to disable such kind of interaction you can try the following:

  1. Make an extension for your UITableViewCell object (in YourTableViewCell.swift file, linked to YourTableViewCell.xib):
extension YourTableViewCell: UITextDropDelegate {
  func textDroppableView(_ textDroppableView: UIView & UITextDroppable, proposalForDrop drop: UITextDropRequest) -> UITextDropProposal {
    return UITextDropProposal(operation: .cancel)
  }
}
  1. Make YourTableViewCell a drop delegate for a TextView (also in YourTableViewCell.swift)
override func awakeFromNib() {
  super.awakeFromNib()
  YourTextView.textDropDelegate = self
}

Where YourTextView is an outlet for your cell's text view

This will help your app to stop trying to insert text inside UITextView in your TableView.
Hope it helps someone.

Upvotes: 4

Mihcael Smith
Mihcael Smith

Reputation: 129

To disable UITextview and UITextfield drop:

textView.removeInteraction(textView.textDropInteraction!)

Upvotes: -1

Alan Scarpa
Alan Scarpa

Reputation: 3560

To disable the text dragging (where the text "pops out" and can be dragged across the screen), try the following:

if #available(iOS 11.0, *) { 
    textView.textDragInteraction?.isEnabled = false 
}

Upvotes: 38

Related Questions