Custom Fonts in Ionic3

不打扰是莪最后的温柔 提交于 2020-01-04 05:36:19

问题


I am using 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

I have the following font: Ormont_Light.ttf

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.

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

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

Question

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

UPDATE

I add the following:

@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.

UPDATE

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;

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;
}

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.

Where do you keep you .ttf file?


回答1:


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;


来源:https://stackoverflow.com/questions/43490703/custom-fonts-in-ionic3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!