通过编程方式按名称获取FontAwesome unicode值

Programmatically get FontAwesome unicode value by name

本文关键字:获取 FontAwesome unicode 编程 方式按      更新时间:2023-09-26

按照这个回答中概述的步骤,我将光标设置为一个FontAwesome图标。现在,我想通过类名(例如,fa-pencil)将光标设置为任何图标。要做到这一点,我似乎需要能够以编程方式查找给定图标的unicode值。

我知道这些值在font-awesome.css样式表中列出,但如果存在其他方法,我希望避免解析该文件。

这可能吗?

可能晚了,但这将允许你这样做:elt.innerHTML = faUnicode('pencil');

也许它可以帮助其他人搜索同样的东西。

function faUnicode(name) {'use strict';
  // Create a holding element (they tend to use <i>, so let's do that)
  const testI = document.createElement('i');
  // Create a realistic classname
  // - maybe one day it will need both, so let's add them
  testI.className = `fa fa-${name}`;
  // We need to append it to the body for it to have
  //   its pseudo element created
  document.body.appendChild(testI);
  // Get the computed style
  const char = window.getComputedStyle(
    testI, ':before' // Add the ':before' to get the pseudo element
  ).content.replace(/'|"/g, ''); // content wraps things in quotes
                                 //   which we don't want
  // Remove the test element
  testI.remove();
  return char.charCodeAt(0);
}

或在ECMA5中:

function faUnicode(name) {
  var testI = document.createElement('i');
  var char;
  testI.className = 'fa fa-' + name;
  document.body.appendChild(testI);
  char = window.getComputedStyle( testI, ':before' )
           .content.replace(/'|"/g, '');
  testI.remove();
  return char.charCodeAt(0);
}

我已经拼凑了一些有用的东西:

var setCursor = function (icon) {
    var tempElement = document.createElement("i");
    tempElement.className = icon;
    document.body.appendChild(tempElement);
    var character = window.getComputedStyle(
        tempElement, ':before'
    ).getPropertyValue('content');
    tempElement.remove();
    
    var canvas = document.createElement("canvas");
    canvas.width = 24;
    canvas.height = 24;
    var ctx = canvas.getContext("2d");
    ctx.fillStyle = "#000000";
    ctx.font = "24px FontAwesome";
    ctx.textAlign = "center";
    ctx.textBaseline = "middle";
    ctx.fillText(character, 12, 12);
    var dataURL = canvas.toDataURL('image/png')
    $('body').css('cursor', 'url('+dataURL+'), auto');
}

这将使用给定的类创建一个临时元素,然后使用window.getComputedStyle来获取:before伪元素的内容。

谢谢大家的帮助!

你可以使用一个隐藏的div来放置图标。一旦它就位,读取其中的字符,获取其值并将其转换为unicode表示形式。一旦你完成了这些,你就可以在你给出的代码中使用它来将它显示为一个光标。请注意,您必须使用getComputedStyle()来获取应用该图标的CSS值。

你可以这样做:

<div style="display:none;"><i id="fontTest"></i></div>

JS

function onSubmit() {
    var userValue = document.getElementById("#someElement").value;
    var fontTest = document.getElementById("#fontTest");
    fontTest.className = fontTest.className + " " + userValue;
    var style = window.getComputedStyle(fontTest);
    var character = String.fromCharCode(style.getPropertyValue("contents"));
    // The character value is now the unicode representation of the icon
}

一个包含缓存的获取字符的版本,这样我们就不必为每次查找创建和删除元素了:

使用

const unicodeCharacter = getIconUnicode("fas fa-arrow-up");

我把它放在一个名为utils.js或utils的文件中。. ts和导出getIconUnicode()函数,但你可以做任何你喜欢的方式,只要你能调用该函数。

ES6

const iconUnicodeCache = {};
const getIconUnicode = (iconClass) => {
    if (iconUnicodeCache[iconClass]) return iconUnicodeCache[iconClass];
    const tempElement = document.createElement("i");
    tempElement.className = iconClass;
    document.body.appendChild(tempElement);
    const character = window.getComputedStyle(tempElement, ':before').getPropertyValue('content').replaceAll(`"`, "");
    tempElement.remove();
    if (character) {
        iconUnicodeCache[iconClass] = character;
    }
    return character;
};

const iconUnicodeCache: {[key: string]: string} = {};
const getIconUnicode = (iconClass: string): string => {
    if (iconUnicodeCache[iconClass]) return iconUnicodeCache[iconClass];
    const tempElement = document.createElement("i");
    tempElement.className = iconClass;
    document.body.appendChild(tempElement);
    const character = window.getComputedStyle(tempElement, ':before').getPropertyValue('content').replaceAll(`"`, "");
    tempElement.remove();
    if (character) {
        iconUnicodeCache[iconClass] = character;
    }
    return character;
};