9

I am working on an app that loads small chunks of HTML into several webviews. I want this HTML to be selectable by the user, so they can copy and paste portions of it. This is working totally fine, but only when the content is greater than 100px tall. For some reason, if it is any shorter, the content can not be selected by the user.

Steps to reproduce:

  1. Create a UIWebView with a width of 322, and a height of 100 and add it to a view
  2. Call loadHTMLString: "W " * 64, baseURL: nil on the webview. (on my screen this is 4 complete rows of "W"s)
  3. Click and hold on one of the "W", note that the copy/paste menu does not appear
  4. Call loadHTMLString: "W " * 65, baseURL: nil on the webview. (on my screen the last "W" is on the 5th row)
  5. Click and hold on one of the "W", note that the copy/paste menu does appear

I've tried a number of thing, to understand what is happening here. I you follow the same steps, but start with a height of 101 instead of 100, it works perfectly find with only 4 rows.

I think this might be a bug in iOS? Maybe?

I'm running iOS 9.2, with Xcode 7.2.1

Does anyone here know why this might be happening, or if there is any sort of work around that I can use to fix this?

4
  • can you replicate the bug on actual device? simulators do not always work 100% correctly.
    – OlDor
    Mar 13, 2016 at 14:28
  • I have replicated this on an actual device. Problem persists.
    – BananaNeil
    Mar 18, 2016 at 15:45
  • Any reason why you can't set the web view to 101?
    – Pradeep K
    Mar 18, 2016 at 16:41
  • My designer requires that these sections are the size of their content. There might be a work around that would involve each webview to be bigger than their content with a super view that clips bounds... but then the scrolling of the webview would have to be stopped.
    – BananaNeil
    Mar 18, 2016 at 16:51

3 Answers 3

3
+50

I've tried a few workarounds, to no avail.

The only workaround I've found: use WKWebView-- it does not have this issue.

Currently, as of iOS 9, you cannot add WKWebView through Interface Builder. This is because it is not supported. You won't be able to include it in IB, as long as WKWebView header declares it as NS_UNAVAILABLE:

- (instancetype)initWithCoder:(NSCoder *)coder NS_UNAVAILABLE;

Instead, you can declare it as a property, and instantiate it in your viewDidLoad implementation, like so:

self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 200, 320, 100)];
1
  • I'm going to go ahead and mark this as the right answer, because it does work (most of the time). It was not an entirely easy switch from UIWebView to WKWebView, but it seems like it might be the right thing to do.
    – BananaNeil
    Mar 18, 2016 at 16:55
1

Well, webview suppose to load html code, but not pure string, that might confuse it. If you add <!DOCTYPE HTML> in front, it can be selected no matter how short the string is.

2
  • This does not actually seem to be true.
    – BananaNeil
    Mar 18, 2016 at 15:43
  • I tried it, it works for me. Do you write it like this? loadHTMLString: "<!DOCTYPE HTML><html>What ever W you want</html>", baseURL: nil
    – Terence
    Mar 18, 2016 at 19:26
0

Setting the scalesPageToFit to true for the web view seems to solve the problem but only if the height is 100. If its 99 or less then same problem persists. But its strange to see the relation between scalesPageToFit and this feature and for a height of 100. Looking at the heights of 101,100,99 it seems like some kind of floating point calculation issue going on here. Definitely a bug.

Upon more investigation I found that if you set the height to 100.5 then the content is selectable. Seems like some kind of rounding off issue in the web view implementation.

When I looked at the web view's scroll view's contentSize it seems like when the scrollview contentSize goes beyond 100 the selection works. So if you handle webviewDidFinishLoad and there you check if the contentSize is < 100 then you can reload the string by appending <br>. As long as its less than 100 you keep on loading adding a <br> every time to the loaded string. This should work for you.

func webViewDidFinishLoad(webView: UIWebView) {
    print(webview.scrollView.contentSize)
    var content = webview.stringByEvaluatingJavaScriptFromString("document.body.innerHTML")
    if content != nil && webview.scrollView.contentSize.height <= 100 {
        content! += "<br>"
        webview.loadHTMLString(content!, baseURL: nil)
    }
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.