Learn Courses My Dashboard

Add imageview below the text

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.

Hello, have u ever used view provider or layout for NSTextAttachment? I need help, cause on devices with iOS15+ my layout is now broken (all is good on ios14). And its hard for me to figure out how to fix it.

I never used NSTextAttachment nor view provider or layout.