Search code examples
javascripthtmluiwebviewselectionhighlighting

UIWebView Text Highlighting solution


Has anyone found a solution for highlighting user-selected blocks of text in a UIWebView?

I have a partial solution in place, however I have been unable to get it to work when the selection includes multiple elements in the DOM. In that case, I can find and highlight the first element in the selection, but javascript seems unable to tell me where the end of the selection is in this case.

It may be that I just don't understand the selection objects completely. It's difficult to find good documentation on them.


Solution

  • You can use document.execCommand("HiliteColor"). I answered a similar question recently:

    Making Selection & Adding tag dynamically in JavaScript

    Regarding documentation, MDN is pretty good for Selection and Range, although it doesn't really go into detailed examples.