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