Attachment in UITextView

I have issue. I'd like to place attachment (like on attached picture; it's Notes on iOS) in UITextView. I tried use NSTextAttachment, but on iOS I couldn't use NSTextAttachmentCell. I'd like to attachment will be part of text. I've tried use exclusion paths, but custom view wasn't be a part of text. Thank you.

If you want to add inside the text, you should use attributedString and NSTextAttachment:

See example here: https : / / w w w.hackingwithswift. com/example-code/system/how-to-insert-images-into-an-attributed-string-with-nstextattachment

Can I make something like 'cell', which will be shows attachment like on attached image?

So, that's not a question about UITextView.

To get this, just define a custom cell. In its nib, you will position label(s) and image, at the exact position you wish.

Ok, I understand. I’ve thought about it, but can I place .xib into UITextView?

No, you can't and that does not make much sense. Where would it be inside the text ? Do not insert the xib, but just an imageView: What you can do:

  • embed UITextView in a UIView (or a stackView)
  • insert imageView inside this UIView.
  • set constraints to position as you want

You can only place image based attachments in UITextView. These aren't interactive.

If you do want interactivity, you need to:

  1. create a placeholder attachment with the right size
  2. track that size and location
  3. add a view to the textview to handle your interactive attachment, keeping the size and location of the view in sync with the attachment
  4. update the view's location (and size) when the text changes, and make sure to delete the view when the attachment is deleted
Attachment in UITextView
 
 
Q