0

I need to save & highlight the selected text.

I get the range as said in Calculate Position of selected text javascript/JQuery?

But, how to highlight the user's selected text on next session using the above stored value?

Note: I went through Rangy's serializer module (demo)

but it consumes much time on iOS UIWebView on loading large content of html in a web view.... App Freezes:-( Also multiple highlighting is not available in Rangy's serializer module.

So What I'm looking for is, highlight based on the start & end offset of range value stored.

Community
  • 1
  • 1
Mani
  • 2,599
  • 4
  • 30
  • 49
  • Is there a way to store node, start value & end value of user selected text to highlight it at next session? – Mani Nov 17 '11 at 10:18
  • see this links: [link1](http://stackoverflow.com/questions/6903292/uiwebview-css-injection-using-javascript) and [link2](http://stackoverflow.com/questions/4579304/how-do-i-expand-the-blue-highlighted-text-in-uiwebview) – NrNazifi Dec 08 '11 at 15:28

1 Answers1

0

If you are rendering the html file, then you might be able to do it in the way below. It highlights the user text then saves it back by rewriting the html file thus preserving the highlight.

 NSString *uuid = [[NSUUID UUID] UUIDString];

 NSString *insertSpan = [NSString stringWithFormat:@"var range = window.getSelection().getRangeAt(0);var selectionContents   = range.extractContents();alert(selectionContents); var span                = document.createElement(\"span\");span.appendChild(selectionContents);span.setAttribute(\"class\",\"uiWebviewHighlight\");span.style.backgroundColor  = \"#99FF00\";span.setAttribute(\"id\", \"%@\");range.insertNode(span);",uuid];
 [webView stringByEvaluatingJavaScriptFromString:insertSpan];

 NSString *document = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].innerHTML"];

 NSData *data = [document dataUsingEncoding:NSUTF8StringEncoding];

 [data writeToFile:<file location> atomically:YES];
Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73
Akhil
  • 54
  • 6