Skip to content

Instantly share code, notes, and snippets.

@CibelePaulinoAndrade
Last active July 27, 2019 01:10
Show Gist options
  • Save CibelePaulinoAndrade/9340653f3f45af267a49f794f87605b6 to your computer and use it in GitHub Desktop.
Save CibelePaulinoAndrade/9340653f3f45af267a49f794f87605b6 to your computer and use it in GitHub Desktop.
extension ViewController: UITextDragDelegate {
func textDraggableView(_ textDraggableView: UIView & UITextDraggable, dragPreviewForLiftingItem item: UIDragItem, session: UIDragSession) -> UITargetedDragPreview? {
let dragView = textDraggableView
let selectionRange = textDraggableView.selectedTextRange
let selectionRects = textDraggableView.selectionRects(for: selectionRange!) as! [UITextSelectionRect]
var array: [CGRect] = []
for r in selectionRects {
array.append(r.rect)
}
let paramaters = UIDragPreviewParameters(textLineRects: array as [NSValue])
let textViewCopy = UITextView(frame: textDraggableView.frame)
let attributedString = NSMutableAttributedString(attributedString: dragTextView.attributedText)
let fullRange = attributedString.mutableString.range(of: dragTextView.text)
let selectedRange = dragTextView.selectedRange
attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.white, range: fullRange)
attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.blue, range: selectedRange)
textViewCopy.attributedText = attributedString
let target = UIDragPreviewTarget(container: self.view, center: dragTextView.center)
let targeDragView = UITargetedDragPreview(view: textViewCopy, parameters: paramaters, target: target)
return targeDragView
}
func textDraggableView(_ textDraggableView: UIView & UITextDraggable, itemsForDrag dragRequest: UITextDragRequest) -> [UIDragItem] {
self.range = dragRequest.dragRange
if let string = dragTextView.text(in: dragRequest.dragRange) {
let itemProvider = NSItemProvider(object: string as NSString)
return [UIDragItem(itemProvider: itemProvider)]
}else{
return []
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment