用javascript计算字母大小 [英] Calculate letter size in javascript

查看:106
本文介绍了用javascript计算字母大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在JavaScript中计算字母的确切大小。该字母可以有不同的字体大小或字体家族的属性等。

我试图使用div元素为此,但此方法只给出div的大小,而不是信。

 < div style =display:inline; background-color:yellow; font-size:53px;行高:32PX>一种与LT; / DIV> 

有人知道如何解决这个问题吗?

解决方案

对于一般情况,这基本上是不可能的。字体字距会根据操作系统,浏览器等等导致字母变化的宽度,并且根据哪些字母彼此相邻。如果os +浏览器没有您指定的字体,则可能会发生字体替换。



也许用你所追求的更高层次的目标重新提出这个问题,可能会导致对你的问题提出其他的解决方法,这可能会更有成果? >

I need to calculate the exact size of the letter in javascript. The letter can have different font-size or font-family attributes, etc.

I tried to use div element for this, but this method gives only the size of the div, not letter.

<div style="display: inline; background-color: yellow; font-size: 53px; line-height:32px">A</div>

Does anybody know how to solve this issue?

解决方案

This is basically not possible for the general case. Font kerning will result in variable "widths" of letters depending on the operating system, browser, etc etc, and based on which letters are next to each other. Font substitution may happen if the os+browser don't have the font you specify.

Perhaps re-asking the question with the higher-level goal you're shooting for might result in proposed other approaches to your problem that might be more fruitful?

这篇关于用javascript计算字母大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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