简体   繁体   中英

Storing the highlighted text locations of UIWebView in a Database

I am working on an eReader project where users can highlight the text of their interest. I have been able to highlight the text by this code:

- (void)markHighlightedString:(id)sender {

// The JS File
NSString *filePath  = [[NSBundle mainBundle] pathForResource:@"HighlightedString" ofType:@"js" inDirectory:@""];
NSData *fileData    = [NSData dataWithContentsOfFile:filePath];
NSString *jsString  = [[NSMutableString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];
[self.bookTextWebView stringByEvaluatingJavaScriptFromString:jsString];

// The JS Function
NSString *startSearch   = [NSString stringWithFormat:@"stylizeHighlightedString()"];
[self.bookTextWebView stringByEvaluatingJavaScriptFromString:startSearch];}

stylizeHighlightedString() is:

function stylizeHighlightedString() {

var range               = window.getSelection().getRangeAt(0);
var selectionContents   = range.extractContents();
var span                = document.createElement("span");

span.appendChild(selectionContents);

span.setAttribute("class","uiWebviewHighlight");
span.style.color            = "red";

range.insertNode(span);}

Now, I want to persist this highlighted text information in the database so that the previously highlighted text can be shown the next time the application is run.

Is this doable?

You could brake the script you are using into two scripts for this. The first one would query for the selected range and return it . You could than parse the returned range and create an (Obj-)C object for it and store it. The second script would do the actual highlighting of the provided range / ranges. For that to happen you need to dynamically compose the javascript by pre-prending the ranges as javascript variables to the javascript string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM