Ionic3 中的自定义字体 [英] Custom Fonts in Ionic3

查看:32
本文介绍了Ionic3 中的自定义字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Ionic3:

Your system information:

Cordova CLI: 6.4.0 
Ionic Framework Version: 3.0.1
Ionic CLI Version: 2.1.18
Ionic App Lib Version: 2.1.9
Ionic App Scripts Version: 1.3.0
ios-deploy version: Not installed
ios-sim version: Not installed
OS: macOS Sierra
Node Version: v6.9.4
Xcode version: Xcode 8.3.1 Build version 8E1000a

我有以下字体:Ormont_Light.ttf

正如您在上面看到的,我尝试在 app.scss 文件中应用这个新字体.但是,我似乎无法使其生效.

As you can see above, I try apply this new font in the app.scss file. However, I cannot seem to make it take effect.

如您所见,它仍然使用 Ionic 默认值:

As you can see, it is still using the Ionic default of:

font-family: "Roboto", "Helvetica Neue", sans-serif;

问题

请任何人建议如何在 Ionic 3 中使用 ttf 文件实现新字体?

Please can anyone advise how to implement a new font using a ttf file in Ionic 3?

更新

我添加以下内容:

@font-face {
font-family: Ormont_Light;
src: url(../assets/fonts/Ormont_Light.ttf) format("ttf");
 font-weight: normal;
 font-style: normal;
}
body {
    font-family: Ormont_Light !important;
}

现在我在源代码中显示了字体:

Now I get the font showing up in the source:

body {
    font-family: Ormont_Light !important;
} 

但它并没有像预期的那样在全球范围内应用到应用程序.

But it's not being applied to the app on a global level as expected.

更新

variables.scss

$font-family-base: "Ormont_Light";
$font-family-ios-base: $font-family-base;
$font-family-md-base: $font-family-base;
$font-family-wp-base: $font-family-base;

谢谢,这样的作品.它现在也应用了 Ormont_Light 我所有的样式,这很棒.即 dom 现在有:

Thanks, That kind of works. It is now applying Ormont_Light too all my styles which is great. i.e. the dom now has:

body {
    font-family: Ormont_Light !important;
}

但显示的字体使用的是 Times New Roman 字体,我猜这是浏览器在找不到引用的字体时的默认设置.所以我猜我的 .ttf 文件的路径不正确.

But the the displayed font is using Times New Roman font, which I guess is the browser default when it cannot find the font referenced. So I guess my path to my .ttf file is incorrect.

.ttf 文件保存在哪里?

推荐答案

你需要重写 离子 Sass 变量 .您需要在 src/theme/variables.scss 中添加此字体.

You need to override the Ionic Sass Variables . You need to add this font-face in src/theme/variables.scss.

@font-face {
 font-family: 'Ormont_Light';
 src:url('../assets/fonts/Ormont_Light.ttf') format('truetype');
 font-weight: normal;
 font-style: normal;
}
$font-family-base: "Ormont_Light";
$font-family-ios-base: $font-family-base;
$font-family-md-base: $font-family-base;
$font-family-wp-base: $font-family-base;

这篇关于Ionic3 中的自定义字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆