Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

like image 891
BalKrishan Yadav Avatar asked Nov 30 '17 14:11

BalKrishan Yadav


2 Answers

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 
}
like image 152
Alan Scarpa Avatar answered Nov 12 '22 18:11

Alan Scarpa


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.

like image 2
grigorevp Avatar answered Nov 12 '22 19:11

grigorevp