3

I have quite straightforward code to add NSTextAttachment to UITextView:

override func viewDidLoad() {
    super.viewDidLoad()
    //textView.textDragInteraction?.isEnabled = false
    textView.becomeFirstResponder()
    textView.isEditable = true
    let image = UIImage(named: "dog")
    let textAttachment = NSTextAttachment()
    textAttachment.bounds = CGRect(x: 0, y: 0, width: (image?.size.width)!, height: (image?.size.height)!)
    textAttachment.image = image
    let attrString = NSAttributedString(attachment: textAttachment)
    textView.textStorage.insert(attrString, at: 252)
}

Works fine except when dragging image attachment UITextView becomes unresponsive, cursor disappears and it's not possible to drop attachment at desired position:

dragging image attachment

Slightly modifying code to:

textAttachment.contents = UIImagePNGRepresentation(image!) as Data?

attached image is not correct, but the rest works as expected, cursor position is visible, attachment drops at desired position.

dragging content attachment

Is it a bug? I have spend quite a lot of time finding the solution, unsuccessfully... I need draggable image attachment... Any help? Thanks

wiskis
  • 91
  • 2
  • 5

0 Answers0