154

我喜欢Font Awesome图标字体,并希望将它用于我网站上的大多数图标,但除了提供的内容之外,我还需要一些自定义 svg 图标。

我注意到.svg 版本的 Font Awesome主要由以下<glyph />元素组成:

...
<glyph unicode="&#xf0b2;" horiz-adv-x="1536" d="M0 80v477q0 51 24.5 61.5t59.5 -24.5l162 -162l340 338l-338 338l-164 -164q-35 -35 -59.5 -25.5t-24.5 60.5v477q0 31 23 57q27 23 57 23h477q51 0 61.5 -24.5t-24.5 -59.5l-160 -158l338 -338l332 334l-162 162q-35 35 -24.5 59.5t61.5 24.5h477q33 0 55 -23 q25 -25 25 -57v-477q0 -51 -24.5 -61.5t-59.5 24.5l-162 162l-334 -334l338 -336l158 160q35 35 59.5 24.5t24.5 -61.5v-477q0 -35 -25 -55q-23 -25 -55 -25h-477q-51 0 -61.5 24.5t24.5 59.5l166 166l-336 336l-340 -340l162 -162q35 -35 24.5 -59.5t-61.5 -24.5h-477 q-31 0 -55 25q-25 20 -25 55z" />
<glyph unicode="&#xf0c0;" horiz-adv-x="1880" d="M0 852v152q0 18 1 47.5t10 56.5t29.5 46.5t57.5 19.5q-45 29 -71.5 75.5t-26.5 104.5q0 43 16.5 82t46 68.5t68.5 46t82 16.5q45 0 84 -16.5t67.5 -46t46 -68.5t17.5 -82q0 -57 -27.5 -104t-72.5 -76q37 0 57.5 -19.5t29.5 -46.5t11 -56.5t2 -47.5v-152 q-14 -8 -23.5 -18.5t-27.5 -10.5h-328q-16 0 -26.5 10.5t-22.5 18.5zM158 57v387q0 78 45 138.5t98 109.5q10 10 25.5 21.5t33.5 15.5q18 6 41 7t45 5q61 10 130 19.5t135 19.5q-90 57 -144.5 151.5t-54.5 207.5q0 88 34 166.5t92 136t136 91.5t166 34t166 -34t136 -91.5 t92 -136t34 -166.5q0 -113 -54 -207t-145 -152q66 -10 134.5 -19t130.5 -20q23 -4 45 -5t41 -7q18 -4 33.5 -15.5t27.5 -21.5q66 -59 103.5 -116.5t37.5 -131.5v-387q-12 -6 -20 -13t-18.5 -14t-23.5 -14.5t-36 -15.5h-1368q-35 0 -54.5 22.5t-43.5 34.5zM1452 852v152 q0 18 2 47.5t11.5 56.5t30 46.5t56.5 19.5q-45 29 -72.5 75.5t-27.5 104.5q0 43 16.5 82t46 68.5t68.5 46t84 16.5q43 0 82 -16.5t68.5 -46t46 -68.5t16.5 -82q0 -57 -26.5 -104t-71.5 -76q37 0 56.5 -19.5t28.5 -46.5t11 -56.5t2 -47.5v-152q-12 -8 -22.5 -18.5 t-26.5 -10.5h-328q-18 0 -27.5 10.5t-23.5 18.5z" />
<glyph unicode="&#xf0c1;" horiz-adv-x="1597" d="M0 1137q0 88 34 166.5t92 137t136 92.5t168 34q86 0 166 -33t139 -92q8 -8 21.5 -20.5t26 -25t21.5 -25.5t9 -25q0 -18 -12 -31q-6 -8 -25 -12q-47 -10 -88 -22.5t-86 -31.5q-4 -4 -16 -4t-25.5 10.5t-31 21.5t-42 21.5t-57.5 10.5q-35 0 -66.5 -13.5t-54 -37t-36 -54 t-13.5 -67.5q0 -41 17.5 -75t43 -63.5t56.5 -56.5t57 -53l180 -178q23 -25 54.5 -37t66.5 -12q43 0 73 16t46 16q12 0 39 -21.5t55.5 -49t50 -55t21.5 -42.5q0 -29 -36 -51t-83 -38.5t-94 -26t-72 -9.5q-86 0 -164.5 33t-140.5 92l-303 305q-61 59 -94 139.5t-33 166.5z M578 1010q0 29 35.5 51t82.5 38.5t94 26t72 9.5q86 0 166 -33t139 -92l303 -305q61 -59 94 -139.5t33 -166.5q0 -90 -33.5 -167.5t-92 -136t-137.5 -92.5t-167 -34q-86 0 -165.5 34t-139.5 93q-8 8 -21.5 19.5t-25.5 25t-21.5 26.5t-9.5 26q0 18 13 28q6 8 24 12 q47 10 88 22.5t86 33.5q12 4 17 4q12 0 25.5 -10.5t30.5 -21.5t41 -21.5t58 -10.5q72 0 121 49.5t49 120.5q0 41 -17 76t-44 63.5t-56.5 55.5t-56.5 53l-178 180q-53 49 -123 50q-43 0 -72.5 -17.5t-46.5 -17.5q-12 0 -38.5 21.5t-55 49t-50 56t-21.5 41.5z" />
...

将我的 svg 图标代码粘贴为新的字形元素并分配一个未使用的 unicode 字符是否有效?

4

7 回答 7

133

试试Icomoon 。您可以上传自己的 SVG,将它们添加到库中,然后创建自定义字体,将 FontAwesome 与您自己的图标结合起来。

于 2013-07-16T03:05:29.043 回答
38

In Font Awesome 5, you can create custom icons with your own SVG data. Here's a demo GitHub repo that you can play with. And here's a CodePen that shows how something similar might be done in <script> blocks.

In either case, it simply involves using library.add() to add an object like this:

export const faSomeObjectName = {
  // Use a prefix like 'fac' that doesn't conflict with a prefix in the standard Font Awesome styles
  // (So avoid fab, fal, fas, far, fa)
  prefix: string,
  iconName: string, // Any name you like
  icon: [
    number, // width
    number, // height
    string[], // ligatures
    string, // unicode (if relevant)
    string // svg path data
  ]
}

Note that the element labelled by the comment "svg path data" in the code sample is what will be assigned as the value of the d attribute on a <path> element that is a child of the <svg>. Like this (leaving out some details for clarity):

<svg>
  <path d=SVG_PATH_DATA></path>
</svg>

(Adapted from my similar answer here: https://stackoverflow.com/a/50338775/4642871)

于 2018-05-14T22:11:12.713 回答
27

我建议将您的图标与FontAwesome分开,并创建和维护您自己的自定义库。就个人而言,我认为如果您要创建自己的图标库,保持FontAwesome的独立性会容易得多。然后,您可以将 FontAwesome 从 CDN 加载到您的网站中,而不必担心保持最新状态。

创建您自己的自定义图标时,请通过Adob​​e Illustrator或类似软件创建每个图标。创建图标后,将每个图标以SVG格式分别保存在计算机上。

接下来,前往IcoMoon: http: //icomoon.io,它拥有最好的字体生成软件(在我看来),而且它是免费的。IcoMoon将允许您将个人 svg 保存的字体导入字体库,然后在eotttfwoffsvg. IcoMoon不生成的一种格式是woff2.

IcoMoon生成图标包后,前往FontSquirrelhttp ://fontsquirrel.com并使用他们的字体生成器。使用您在IcoMoonttf生成的文件。在创建的新生成的图标包中,您现在将拥有格式的图标包。woff2

确保eotttfsvgwoff和的文件woff2都同名。您正在从两个不同的网站/软件生成一个图标包,它们确实以不同的方式命名它们生成的输出。

您将在这两个位置为您的图标包生成 CSS。但在IcoMoon生成的 CSS不会在您的声明中包含该woff2格式。@font-face {}确保在将 CSS 添加到项目时添加:

@font-face {
    font-family: 'customiconpackname';
    src: url('../fonts/customiconpack.eot?lchn8y');
    src: url('../fonts/customiconpack.eot?lchn8y#iefix') format('embedded-opentype'),
         url('../fonts/customiconpack.ttf?lchn8y') format('truetype'),
         url('../fonts/customiconpack.woff2?lchn8y') format('woff'),
         url('../fonts/customiconpack.woff?lchn8y') format('woff'),
         url('../fonts/customiconpack.svg?lchn8y#customiconpack') format('svg');
    font-weight: normal;
    font-style: normal;
}

请记住,您可以使用IcoMoon软件获取图标包中每个图标的字形 unicode 值。这些值有助于通过 CSS 分配图标,如(假设我们使用上面font-family示例中的声明@font-face {...}):

selector:after {
    font-family: 'customiconpackname';
    content: '\e953';
    }

如果您在文本编辑器中e953打开字体包生成的文件,您还可以获得字形 unicode 值。svg例如:

<glyph unicode="&#xe953;" glyph-name="eye" ... />
于 2016-12-22T16:55:15.427 回答
19

取决于你有什么。如果您的 svg 图标只是一个路径,那么只需将 'd' 属性复制到新的 <glyph> 元素即可轻松添加该字形。但是,路径需要缩放到字体的坐标系(EM-square,通常是 [0,0,2048,2048] - Truetype 字体的标准)并与您想要的基线对齐。

但是,并非所有浏览器都支持 svg 字体,因此如果您希望它在任何地方都可以使用,您也必须将其转换为其他格式。

Fontforge可以导入 svg 文件(选择一个字形插槽,文件 > 导入,然后选择您的 svg 图像),然后您可以转换为所有相关的字体格式(svg、truetype、woff 等)。

于 2012-07-11T12:08:24.630 回答
14

与@Samuel-bergström 类似的方法:

  • 下载 Fontawesome SVG https://github.com/FortAwesome/Font-Awesome/blob/master/src/assets/font-awesome/fonts/fontawesome-webfont.svg
  • 下载 FontForge http://fontforge.github.io/en-US/downloads/
  • 下载 Inkscape
  • 打开 Inskscape 并创建一个单层形状作为新字体图标
  • 保存 SVG 文件,关闭 Inkscape
  • 打开 FontForge (如果您有多个显示器,请使用 Windows-LeftArrow 重新定位,因为它们有奇怪的 SWING java 窗口关闭显示器,并且弹出窗口有模式问题 - 我必须检查我的任务栏有一些)
  • 文件 | 打开 fontawesome-webfont.svg
  • 文件 | 进口
  • 滚动到底部,右键单击图标 | 字形信息
  • 将字形名称更新为 uniFXXX(XXX 类似于 501,比 FontAwesome v4.5 中使用的最高 Unicode 更高的数字)
  • Unicode 值 U+fXXX
  • 点击确定
  • 文件 | 节省
  • 文件 | 生成字体...
  • 关闭 FontForge
  • 打开您的网络项目
  • 将您的字体文件复制到(在我的项目中)“\Content\fonts\”文件夹。
  • 将“\Content\styles\fa\path.less”编辑为:

@font-face {
      font-family: 'FontAwesome';
      //src: url('@{fa-font-path}/fontawesome-webfont.eot?v=@{fa-version}');
      src: 
    	//url('@{fa-font-path}/fontawesome-webfont.eot?#iefix&v=@{fa-version}') format('embedded-opentype'),
        //url('@{fa-font-path}/fontawesome-webfont.woff2?v=@{fa-version}') format('woff2'),
        url('@{fa-font-path}/fontawesomeregular.woff?v=@{fa-version}') format('woff'),
        url('@{fa-font-path}/fontawesomeregular.ttf?v=@{fa-version}') format('truetype'),
        url('@{fa-font-path}/fontawesomeregular.svg?v=@{fa-version}#fontawesomeregular') format('svg');
    //  src: url('@{fa-font-path}/FontAwesome.otf') format('opentype'); // used when developing fonts
      font-weight: normal;
      font-style: normal;
    }

我知道注释掉其他文件类型可能是“有争议的”,但很高兴听到如何在注释中生成 .eot 或 .otf 文件。

  • 最后,正如 Samuel 提到的,更新你的 CSS/LESS:

    .fa-XXX:之前 { 内容:“\f501”;}

于 2016-01-28T05:54:44.677 回答
3

我对 SVG webfonts 和字体创建进行了一些研究,在我看来,如果您想将字体“添加”到 font-awesome 已经存在的字体中,您需要执行以下操作:

转到inkscape并创建一个字形,将其保存为SVG,以便您可以阅读代码,确保为其分配一个当前未使用的unicode字符,这样它就不会与字体中的任何现有字形冲突。这可能很难,所以我认为一个更好更简单的方法是用你自己的字形替换现有的字形(只需选择一个你不使用的字形,复制第一部分:

保存 svg 然后使用任何在线转换器将其转换为网络字体,这样您的网络字体就可以在所有浏览器中使用。

完成此操作后,您应该拥有一个替换了其中一个字形的 SVG,在这种情况下您就完成了。如果不是,您需要为新字形创建 CSS,在这种情况下,请尝试重用 FA 现有的 CSS,并且只添加

>##CSS##
>.FA.NEW-GLYPH:after {
>content:'WHATEVER AVAILABLE UNICODE CHARACTER YOU FOUND'
>(other conditions should be copied from other fonts)
>}
于 2015-05-21T10:09:35.697 回答
3

如果您想要一些来自 font-awesome 的图标(或全部),包括您的自定义 svg 图标,您可以:

1- 转到http://fontawesome.io/下载 zip 并解压缩,例如在您的桌面中。

2- 转到http://fontastic.me/使用您的电子邮件创建一个帐户。

3- 登录后单击标题选项:添加更多图标。

4- 选择位于您提取的 zip inside 字体中的 font-awesome 的 SVG。

5-重复上传您自己的svg文件的过程。

6- 在主页内(在页面的标题处)选择您要下载的图标,自定义它们以提供您的自定义名称,然后选择发布以获得链接或下载字体和 css。

对不起我的英语!:D

于 2017-05-03T11:49:37.603 回答