简体   繁体   English

如何在JavaScript中获得单个字符的最大宽度?

[英]How to get the maximum width of a single character in JavaScript?

How can I get the maximum width of a single character in JavaScript? 如何在JavaScript中获得单个字符的最大宽度? I have a text painted in an unknown font, but I know what the text length and font size is. 我有一个用未知字体绘制的文本,但我知道文本长度和字体大小是多少。

Now I want the to get the largest character in that font, so I can make sure the parent element (a div for example) is not too narrow (it has an absolute width). 现在我希望得到该字体中最大的字符,所以我可以确保父元素(例如div )不是太窄(它有一个绝对宽度)。 The widest character is usually the M or W, but I'm not sure, perhaps there are fonts which paints wider characters. 最宽的角色通常是M或W,但我不确定,也许有字体可以画出更广泛的字符。

Now how do I do that? 现在我该怎么做? Is there a fast method for? 有快速的方法吗?

Use the CSS width: 1em; 使用CSS width: 1em; . em is a relative measurement and is the width of the 'm' character em是相对测量值,是“m”字符的宽度

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

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