Calculate text width with JavaScript

前端 未结 24 1617
陌清茗
陌清茗 2020-11-21 05:08

I\'d like to use JavaScript to calculate the width of a string. Is this possible without having to use a monospace typeface?

If it\'s not built-in, my only idea is t

24条回答
  •  -上瘾入骨i
    2020-11-21 05:18

    The ExtJS javascript library has a great class called Ext.util.TextMetrics that "provides precise pixel measurements for blocks of text so that you can determine exactly how high and wide, in pixels, a given block of text will be". You can either use it directly or view its source to code to see how this is done.

    http://docs.sencha.com/extjs/6.5.3/modern/Ext.util.TextMetrics.html

提交回复
热议问题