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.

2
  • Is there a way to store node, start value & end value of user selected text to highlight it at next session? Commented Nov 17, 2011 at 10:18
  • see this links: link1 and link2 Commented Dec 8, 2011 at 15:28

1 Answer 1

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];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.