简体   繁体   English

在UIWebView中更改字体大小后重新定位

[英]Repositioning after changing font size in UIWebView

I have an UIWebView with a huge book in it. 我有一个带有大量书籍的UIWebView。 I'm changing it's font size via javascript, using "document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust='150%'; 我正在使用"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust='150%';通过JavaScript更改字体大小"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. HTML页面变大,但是滚动位置保持不变,导致文本移出用户视线。

The only idea that I have, is really weird and inefficient: 我唯一的想法真的很奇怪而且效率很低:

  1. Wrap every word in <span> tags; 将每个单词包装在<span>标签中;
  2. find the first onscreen <span> and remember it's id ; 找到第一个屏幕上的<span>并记住它的id
  3. resize font; 调整字体大小;
  4. scroll to span, that I've found in step 2. 滚动到我在步骤2中找到的范围。

Is there a better way to preserve the position, that user was reading? 有没有更好的方法来保留该用户正在阅读的位置?

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: 在更改字体大小之前,我使用一些JavaScript来查找和存储页面上第一个字母的位置:

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. 之后,我更改字体大小,通过XPath查找所需的元素,在我的字母前插入不可见的<a> ,滚动到该不可见的<a> ,不要忘记删除它。

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. 那不是一个简单的想法,但至少它能起作用,并且不会消耗太多的CPU或RAM,就像我在原始问题中已经解释过的想法一样。

Here is the place to get getElementXPath() function 这是获取getElementXPath()函数的地方

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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