发现在屏幕上线的位置 [英] Find location of line on screen

查看:204
本文介绍了发现在屏幕上线的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,我已被呈现在浏览器的HTML文本(在这种情况下,一个Android的WebView)。我想找出(X,Y)的文本中的任何给定行的像素位置的它呈现什么。我使用的线的工作定义不只是所有包含在&LT的文本; P> 标记或&LT之前出现; BR> 标记。我的意思是一条线,因为它会显示给用户。

So I have HTML text being rendered in a browser (in this case an Android WebView). I want to find out what the (x,y) location in pixels of any given line of text is AFTER it is rendered. The working definition of line I am using is not just all the text contained in a <p> tag or that appears before a <br> tag. I mean a line as it would appear to the user.

我愿意接受任何建议的方法。

I am open to any suggested method.

有没有,你是能够找到一个div线和各自的高度数的CSS属性?这将提供一个可行的解决方案。

Is there any CSS property that you are able to find the number of lines in a div and their respective heights? That would provide a workable solution.

谢谢!

推荐答案

您不能单独访问线。但是,与一些JavaScript,可以发现与已知的索引的线的位置;这里有一个基本轮廓:

You can't access a line individually. But, with some JavaScript, you can find the position of a line with a known index; here's a basic outline:

var p = document.getElementById("ptag"); //get the text container that contains your line
var nthline = 3; //the line for which you'd like to find the position
var lnheight = parseInt(window.getComputedStyle(p).lineHeight); //get the height of each line
var linepos = [p.offsetLeft, p.offsetTop + lnheight * (nthline - 1)]; //a [left, top] pair that represents the line's position

注意:这是假设容器没有什么,但文字

Note: This assumes the container doesn't have anything but text.

这篇关于发现在屏幕上线的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆