2

I'm wondering how to best implement a textView with inline images in my ios app.

I'm currently using a textView which is populated by a plist string. I would like to add some images inline throughout the text and was wondering what would be a good solution. Do I need to make a webView or is there a simpler method?

If anyone can point me in the right direction, that would be fantastic. Thanks.

Galaxy
  • 3,370
  • 3
  • 29
  • 41

1 Answers1

7

A fairly easy way to do this is using the textContainer's exclusion paths to remove the space where the text is able to occupy and placing an imageView there instead. It may look something like this:

let image = UIImageView(image: UIImage(named: "Clock"))
let path = UIBezierPath(rect: CGRectMake(0, 0, image.frame.width, image.frame.height))
textView.textContainer.exclusionPaths = [path]
textView.addSubview(image)

And to make it read-only:

textView.editable = false
textView.selectable = false
Bhaumik
  • 1,218
  • 1
  • 11
  • 20
Ian
  • 12,538
  • 5
  • 43
  • 62