【问题标题】:NativeScript + Vue.js + FontAwesomeNativeScript + Vue.js + FontAwesome
【发布时间】:2019-02-07 23:52:08
【问题描述】:

我正在尝试使用 FontAwesome 图标集在 NativeScript 和 Vue.js 上构建应用程序,但我无法找出问题所在,因为我什至没有错误提示消息。

我忠实地遵循文档,但没有任何反应。我到处搜索,但……什么都没有。

如果您能帮我解决这个问题,我将不胜感激。

文档:https://nativescript-vue.org/blog/using-fonticons/

谢谢!

解决方案

  • app文件夹中创建一个名为fonts的文件夹
  • 生成样式文件并放入以下代码

.fa { font-family: 'Font Awesome 5 Free', 'font-awesome/fa-regular-400'; }
.fas { font-family: 'Font Awesome 5 Free', 'font-awesome/fa-solid-900'; }
.fab { font-family: 'Font Awesome 5 Free', 'font-awesome/fa-brands-400'; }

font-family第一部分是给字体家族命名,在逗号后面,我们会写到那些字体的路径。

【问题讨论】:

    标签: vue.js nativescript font-awesome nativescript-vue


    【解决方案1】:

    要让 Brands 和 Pro FontAwesome 5 字体在 NativeScript-Vue 中工作,请安装 nativescript-fonticonnpm install nativescript-fonticon --save,如文档 Using Fonticons 中所述, 并从 FontAwesome 下载 webfonts 和桌面字体。在目录app/fonts 中添加来自Web 下载的webfonts 目录的.ttf 文件。 iOS 还需要桌面下载的otfs 目录中的.otf 文件,因此也将这些文件添加到app/fonts 目录并重命名.otf 文件的基本名称以匹配.ttf 文件的相应基本名称

    app/assets添加网页字体下载的css目录下对应的css文件(或all文件)。

    现在将以下内容添加到app.scss(请注意,如果没有正确定义font-weight,light 和solid 将无法正常工作)

    .fa {
      font-family: "Font Awesome 5 Pro", "fa-regular-400";
      font-weight: 400;
    }
    
    .fas {
      font-family: "Font Awesome 5 Pro", "fa-solid-900";
      font-weight: 900;
    }
    
    .fab {
      font-family: "Font Awesome 5 Brands", "fa-brands-400";
      font-weight: 400;
    }
    
    .fal {
      font-family: "Font Awesome 5 Pro", "fa-light-300";
      font-weight: 300;
    }
    

    以下内容发送至main.ts

    import {TNSFontIcon, fonticon} from 'nativescript-fonticon';
    
    TNSFontIcon.debug = true;
    TNSFontIcon.paths = {
      // 'fa': './assets/css/all.min.css',
      // 'fal': './assets/css/all.min.css',
      // 'far': './assets/css/all.min.css',
      // 'fas': './assets/css/all.min.css',
      // 'fab': './assets/css/all.min.css',
      'fa': './assets/css/fontawesome.min.css',
      'fal': './assets/css/light.min.css',
      'far': './assets/css/regular.min.css',
      'fas': './assets/css/solid.min.css',
      'fab': './assets/css/brands.min.css'
    };
    TNSFontIcon.loadCss();
    
    Vue.filter('fonticon', fonticon);
    

    现在删除platforms 目录,以确保所有内容都正确捆绑,您应该一切顺利。像这样使用它

      <StackLayout orientation="horizontal" horizontalAlignment="center" verticalAlignment="top">
        <Label class="fab" :text="'fa-git' | fonticon" />
        <Label class="fal" :text="'fa-plus-square' | fonticon" />
        <Label class="fa" :text="'fa-plus-square' | fonticon" />
        <Label class="fas" :text="'fa-plus-square' | fonticon" />
      </StackLayout>
    

    为了让事情变得更简单,插件Vue-Fonticon 本质上是我复制到app/components/FontIcon.vue 中的以下代码

    <template>
      <Label
        :class="type"
        :color="color"
        :fontSize="size"
        :text="name | fonticon"
        :width="size"
        textAlignment="center"
        @tap="$emit('tap')"
      />
    </template>
    
    <script>
      export default {
        name: 'FontIcon',
        props: {
          color: {
            type: String,
            default: 'black'
          },
          name: {
            type: String,
            required: true
          },
          size: {
            type: [String, Number],
            default: 15,
            validation: s => !isNaN(s)
          },
          type: {
            type: String,
            default: 'fa'
          }
        }
      }
    </script>
    

    要使用它,在main.ts添加

    import FontIcon from './components/Utility/FontIcon.vue'
    
    Vue.component(FontIcon.name, FontIcon)
    
    

    并将其用作

      <StackLayout orientation="horizontal" horizontalAlignment="center" verticalAlignment="top">
        <FontIcon name="fa-play"/>
        <FontIcon name="fa-play" type="fal"/>
        <FontIcon name="fa-play" type="fas"/>
        <FontIcon name="fa-git" type="fab"/>
      </StackLayout>
    

    【讨论】:

    【解决方案2】:

    使用 FontAwesome 4.7.0 对我有用。 从 5.0 版本开始,font awesome 的封装发生了变化,不再推荐使用 ttf 的方式,也许在 nativescript-vue 中使用 font awesome 5.x 比较棘手。

    你可以在那里下载:https://fontawesome.com/v4.7.0/

    按照文档中的说明使用 fontawesome-webfont.ttf 并将其重命名为 FontAwesome.ttf,也复制 font-awesome.min.css

    在您的 app.scss 中,不要忘记这一点(字体系列是您的 ttf 文件的名称)

    .fa {
       font-family: FontAwesome;
    }
    

    记得检查 v4.7 上的图标名称,有些是 v5 中的新名称

    在调试模式下,当您的应用启动时,您应该会看到:

    JS: 'Collections to load: fa'
    JS: '----------'
    JS: 'Loading collection \'fa\' from file: ./fonts/FontAwesome.css'
    JS: 'fa-glass: \\uf000'
    JS: 'fa-music: \\uf001'
    JS: 'fa-search: \\uf002'
    JS: 'fa-envelope-o: \\uf003'
    ...
    

    【讨论】:

    • 使用nativescript-vue.org/blog/using-fonticons 和上面@user3546128 帖子中描述的解决方案。不要忘记在字体真棒免费图标中,很多图标不存在于常规中,因此您将不得不使用“fas”类而不是“fa”。如果您的图标在免费版本中是常规的还是实心的,请查看 fontawesome 网站。我花了一些时间才弄清楚为什么我的图标没有显示...
    猜你喜欢
    • 2020-05-24
    • 1970-01-01
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    • 2020-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多