JavaScript中如何获取单个字符的最大宽度?
如何获取JavaScript中单个字符的最大宽度?我有一个用未知字体绘制的文本,但我知道文本长度和字体大小是多少。
现在我希望获得该字体中最大的字符,因此我可以确保父元素(例如 div
)不会太窄(它具有绝对宽度)。最宽的字符通常是 M 或 W,但我不确定,也许有些字体可以绘制更宽的字符。
现在我该怎么做呢?有没有快速的方法呢?
How can I get the maximum width of a single character in 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). The widest character is usually the M or W, but I'm not sure, perhaps there are fonts which paints wider characters.
Now how do I do that? Is there a fast method for?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 CSS
width: 1em;
。em
是相对测量值,是“m”字符的宽度Use the CSS
width: 1em;
.em
is a relative measurement and is the width of the 'm' character