1

I have an UIWebView with a huge book in it. I'm changing it's font size via javascript, using "document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust='150%';

Html-page gets larger, but the scroll position remains the same, causing text to shift out of a users sight.

The only idea that I have, is really weird and inefficient:

  1. Wrap every word in <span> tags;
  2. find the first onscreen <span> and remember it's id;
  3. resize font;
  4. scroll to span, that I've found in step 2.

Is there a better way to preserve the position, that user was reading?

1 Answer 1

1

Finally I've found an acceptable way:

Before changing font size I use a little javascript to find and store a position of a first letter on a page:

var range = document.caretRangeFromPoint(0,0); // get a range of a first onscreen letter
var textContainer = range.startContainer.parentNode;// get an element to which it belongs
var path = getElementXPath(textContainer); // get an XPath for that element (this function is not biult in, but you can find it in some other question)
path+='|'+range.startOffset; // stick XPath and index of the letter together

After that I change the font size, find needed element by XPath, insert invisible <a> right before my letter, scroll to that invisible <a>, don't forget to remove it.

Done. That is not a stragihtforward idea, but at least it works and does not consume to much of CPU or RAM, like the idea that I'have explained in original question.

Here is the place to get getElementXPath() function

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.