如何在我的 SVG 图像中包含 Font Awesome 图标?

问题描述 投票:0回答:3
css svg font-awesome
3个回答
130
投票

i
不是有效的 SVG。您需要包含显示图标的实际字符。如果您查看 font Awesome 的样式表,您会看到...

.icon-group:before                { content: "\f0c0"; }
.icon-link:before                 { content: "\f0c1"; }
.icon-cloud:before                { content: "\f0c2"; }
.icon-beaker:before               { content: "\f0c3"; }
.icon-cut:before                  { content: "\f0c4"; }
.icon-copy:before                 { content: "\f0c5"; }
.icon-paper-clip:before           { content: "\f0c6"; }
.icon-save:before                 { content: "\f0c7"; }
.icon-sign-blank:before           { content: "\f0c8"; }
.icon-reorder:before              { content: "\f0c9"; }
.icon-list-ul:before              { content: "\f0ca"; }
.icon-list-ol:before              { content: "\f0cb"; }
.icon-strikethrough:before        { content: "\f0cc"; }
.icon-underline:before            { content: "\f0cd"; }
.icon-table:before                { content: "\f0ce"; }

但这些是为 CSS 编码的 unicode 字符。在 SVG 中,您需要将示例

\f040
的语法更改为:

<g><text x="0" y="0">&#xf040;</text></g>

然后在样式表中添加:

svg text {
   font-family: FontAwesome;
}

根据 Niek 的评论,如果您使用免费版本的 Font Awesome 5+,则必须使用以下字体系列声明:

svg text {
   font-family: 'Font Awesome 5 Free';
}

27
投票

我的演示

<!DOCTYPE html>
<html>
  <head>
    <script src="http://d3js.org/d3.v3.min.js"></script>
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
    <meta charset="utf-8">
    <title>JS Bin</title>
  </head>
  <body>
    <div title="Code: 0xe800" class="the-icons span3">

      <H3>Standard using:</H3>
    <i class="fa fa-camera-retro fa-4x"></i>
    <br>
      <H3>SVG using:</H3>
  </body>
</html>

JS

var svg = d3.select("body")
  .append("svg")
  .attr("width", 250)
  .attr("height", 250);

svg.append("text")
  .attr("x",0)
  .attr("y",70)
  .attr("font-family","FontAwesome")
  .attr('font-size', function(d) { return '70px';} )
  .text(function(d) { return '\uf083'; }); 

0
投票

下面的代码非常适合我。

   svg.append('tspan')
        .attr('class', 'fa')
        .text('\uf05a')
© www.soinside.com 2019 - 2024. All rights reserved.