我正在尝试做一些基于句子中字符数量的动态规划。英语字母表中哪个字母在屏幕上占像素最多?
当前回答
这取决于字体。例如,交叉0所占的空间要比普通0大得多。
但如果要猜的话,我会选X或B。
其他回答
大写的“M”通常是最宽的。
我认为字母W是最宽的。
或者如果你想要一个宽度的映射,包含不止上面描述的alpha(数字)字符(就像我在非浏览器环境中需要的那样)
const chars = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "!", "\"", "#", "$", "%", "'", "(", ")", "*", "+", ",", "-", ".", "/", ":", ";", "=", "?", "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "\\", "]", "^", "_", "`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~", " ", "&", ">", "<"] const test = document.createElement('div') test.id = "Test" document.body.appendChild(test) test.style.fontSize = 12 const result = {} chars.forEach(char => { let newStr = "" for (let i = 0; i < 10; i++) { if (char === " ") { newStr += " " } else { newStr += char } } test.innerHTML = newStr const width = (test.clientWidth) result[char] = width / 10 }) console.log('RESULT:', result) #Test { position: absolute; /* visibility: hidden; */ height: auto; width: auto; white-space: nowrap; /* Thanks to Herb Caudill comment */ }
Arial 30px在Chrome - W获胜。
根据您的平台,可能有一种方法从字符串或DrawText()函数中“getWidth”,以某种方式使用width属性。
我会做一个简单的算法时间,利用所需的字体,然后通过alfabet运行,并将其存储在一个小配置或只是计算它在初始化作为一个循环从a到Z并不难。