I am giving this answer based on my experience into development of native epub player for android and ios.
I had achieved it in this way .
This is the way I have done text highlighting
- once your url loads into webview in onload call back inject jquery lib to webview dom like this
private void addJQueryJS()
{
String path = "file:///android_asset/JSLibraries/jquery.min.js";
String data = "{\"MethodName\":\"onJQueryJSLoaded\",\"MethodArguments\":{}}";
String callBackToNative = " jsInterface.callNativeMethod('jstoobjc:"+data+"');";
String script = "function includeJSFile()"
+"{"
+"function loadScript(url, callback)"
+"{"
+"var script = document.createElement('script');"
+"script.type = 'text/javascript';"
+"script.onload = function() {"
+"callback();"
+"};"
+"script.src = url;"
+"if(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0])"
+"{"
+"(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(script);"
+"}"
+"else { callback(); }"
+"}"
+"loadScript('"+path+"', function()"
+"{"
+callBackToNative
+"});"
+"} ; includeJSFile();";
loadUrl("javascript: "+script);
}
wrap each word into a span with a unique id
then create a js file and inject same as jquery and write touch methods in it
function onTouchMove(e)
{
e.preventDefault();
if((touchendStartStick == true || touchendEndStick == true) && currHVO != undefined)
{
var touch = e.originalEvent.touches[0] || e.originalEvent.changedTouches[0];
var obj = document.elementFromPoint(touch.pageX-window.pageXOffset,touch.pageY-window.pageYOffset);
if($(obj).is('span'))
{
$(obj).css('background-color','rgba(0,0,255,0.3)');
}
}
};
you can get word x,y width height values also .so you can show start ,end draggable pins on native view.
you can save highlight by saving the first and last word id of an highlight into db or to some persistent storage.Next time when user opens the book get those words ids and highlight those spans .
你希望能够永久保存更改EPUB文件?我认为这是不可能的,因为'WebView'是只读的......您可能可以使用JavaScript,并随时跟踪更改。 –
谢谢你的回应。我不认为WebView能够保存更改。但基本上,我想要做的就像iPad上的iBook阅读器。我想突出或添加评论给epub。我应该用标签来显示epub文本吗? – codingdaddy
我不认为这会太容易;你将不得不制作自己的epub渲染器/查看器。例如,这是[PDF渲染器](http://andpdf.sourceforge.net/);我不确定epub渲染器如何工作。或者,您可以尝试在“WebView”中使用JavaScript接口来更改文档(也可以以某种方式保存这些更改)。 –