Learn Courses My Dashboard

Add imageview below the text

Hello.
Question:
How to add image view below the text in the text view programmatically?

Create an instance of an image view, and add it to the view with view.addSubView(newImageView)
Then add constraints

No, I need to save image straightly below the text.
For example, here:


I want to save the image straightly 2 or 3 cm below the words line where it’s written “Want to”.

So the general idea is to add your text to an NSAttributedString, then use NSTextAttachment to add an image to the attributed string, then assign this to your UITextView's attributedString property.

If you want more control over positioning and such, you will need to use NSTextAttachmentViewProvider and NSTextAttachmentLayout , both of which are only available in the latest versions of iOS. This will involve much more complicated TextKit work and, IMO, is more trouble than it’s worth for what your purposes seem to be. But that’s your call.

This example uses a UILabel but the same principle applies to UITextView. You can google more examples yourself.