I'm developing a book reader for iOS using a UIWebView
. At the moment I'm working with some basic HTML files but will eventually work with ePubs. I am looking for a suitable way of styling ranges of text. My ranges are a little bit special in that they usually encompass three ranges - a keyrange and a range immediately before and range immediately after. The keyrange may span multiple nodes and may start or end for example within a selection of bold text etc. The styling should not be written to file.
At the moment I have a working solution as follows:
document.designMode = "on";
// Color the first section
var selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range1);
if (!selection.isCollapsed){
document.execCommand("foreColor", false, color1);
}
// Color the middle section
selection.removeAllRanges();
selection.addRange(range2);
if (!selection.isCollapsed){
document.execCommand("backColor", false, color2);
document.execCommand("foreColor", false, color3);
}
// Color the last section
selection.removeAllRanges();
selection.addRange(range3);
if (!selection.isCollapsed){
document.execCommand("foreColor", false, color1);
}
document.designMode = "off";
selection.removeAllRanges();
This works fine but is noticeably slow (on an iPad2), even if I modify it to highlight a single range in a short HTML document. There is always a very noticeable delay before the text is stylised. Looking at ebook readers on iPad such as kindle or iBooks there is no discernible delay. How might they implement their highlighting function? Might they be reading the geographical location on the selected text and applying some kind of overlay?
I have searched for a better solution than I'm already using but with no luck so if anyone has an idea I would be very grateful.
Thank you!