要让 Brands 和 Pro FontAwesome 5 字体在 NativeScript-Vue 中工作,请安装 nativescript-fonticon、npm 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>