简体   繁体   English

在JavaScript中获取计算字体系列

[英]Get computed font-family in JavaScript

This is a followup to this question . 这是这个问题的后续内容。

I am working on a component for the CKEditor , a tweaked version of the font drop-down menus that always display the currently selected font family / size values, no matter where they were defined, using computedStyle and consorts. 我正在研究CKEditor的一个组件,这是一个字体下拉菜单的调整版本,它总是显示当前选择的字体系列/大小值,无论它们在何处定义,都使用computedStyle和consorts。

As you can see in the other question, determining the font size works cross-browser now. 正如您在其他问题中所看到的,确定字体大小现在可以跨浏览器工作。 Now I am having trouble working with the fontFamily attribute. 现在我在使用fontFamily属性时遇到问题。 My generic "computed style" function returns only the full font string that was defined, eg 我的通用“计算样式”函数仅返回已定义的完整字体字符串,例如

Times New Roman, Georgia, Serif

What I need, in order to match the setting against the entries in the font family dropdown, is a fixed font name of the actual font of the DOM element I am checking. 为了使设置与字体系列下拉列表中的条目相匹配,我需要的是我正在检查的DOM元素的实际字体固定字体名称

Can this be done somehow, at least for the most common web fonts? 这可以以某种方式完成,至少对于最常见的Web字体?

I don't think there are any methods to do it directly, but Lalit Patel came up with a clever technique that creates an element with some letters in it, and guesses the font from the width of the element. 我认为没有任何方法可以直接进行,但Lalit Patel提出了一种巧妙的技术,可以创建一个带有一些字母的元素,并从元素的宽度猜测字体。

See http://www.lalit.org/lab/javascript-css-font-detect http://www.lalit.org/lab/javascript-css-font-detect

The UA picks the first font in the list that it finds installed. UA选择它找到的列表中的第一个字体。 The fonts installed on the OS are not really a part of the DOM, so the best you can do is guess. 操作系统上安装的字体实际上并不是DOM的一部分,所以你能做的最好就是猜测。

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

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