如果你渲染HTML文件,那麼你也許能夠以下面的方式來完成。它突出顯示用戶文本,然後通過重寫html文件將其保存,從而保留高亮顯示。
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];
有沒有辦法存儲用戶選定文本的節點,起始值和最終值,以在下一個會話中突出顯示它? – Mani
看到這個鏈接: [link1](http://stackoverflow.com/questions/6903292/uiwebview-css-injection-using-javascript) 和 [link2](http://stackoverflow.com/questions/ 4579304 /怎麼辦-I-擴大-的藍色高亮文本功能於UIWebView中) – NrNazifi