1

I would like to know how to build a UITextView with images inserted in the text. I know I can add them as attachment but I would like to introduce a challenge on top of that: I would like to add padding at the text but not at the images, like shown in the picture. (Do not look at the initial image of the capital letter)

How can I do? Thanks

enter image description here

Community
  • 1
  • 1
Nicholas Allio
  • 717
  • 2
  • 9
  • 28

1 Answers1

1

I solved the problem using a UIWebView in place of the UITextView. I render a local HTML page which is easier to customize in these cases. Once you create your own HTML page in your favourite editor, you drag it into your Xcode project. Then you just have to tell your UIWebView the path to the page to be rendered:

    let url = URL(fileURLWithPath: Bundle.main.path(forResource: "<name of html file>", ofType: "html", inDirectory: "<name of the directory>")!)
    self.webView.loadRequest(URLRequest(url: url))

In my case, I put the html page and the images I render in a separate folder imported in the project; then you have to reference that folder when generating the URL.

For a better result I've set scalePageToFit = true and disabled all the other options of the UIWebView. Moreover, in order to disable the zooming by the user, I implemented the UIScrollViewDelegate method viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? to simply returning nil value to a better user experience.

Remember to set self.webView.scrollView.delegate = self

Nicholas Allio
  • 717
  • 2
  • 9
  • 28
  • Hey Nicholas. Facing same issue. It would have been great to have a more detailed answer with an example ;) – Sam Sep 28 '16 at 08:32