简体   繁体   English

自定义字体在Rails上不起作用

[英]Custom Font Not Working on Rails

I have read lots of articles on getting a custom font to work on a rails app, yet I am still having trouble with it. 我已经阅读了很多有关如何在Rails应用程序上使用自定义字体的文章,但是我仍然遇到麻烦。

I downloaded X font from dafont.com. 我从dafont.com下载了X字体。 I unzipped it, and the package contained x.otf. 我将其解压缩,该软件包包含x.otf。 I then installed it on my computer, but when I use it in my app the font is distorted. 然后,我将其安装在计算机上,但是当我在应用程序中使用它时,字体变形了。

I then downloaded Y font from dafont.com. 然后,我从dafont.com下载了Y字体。 I unzipped, it contained y.ttf. 我解压缩,其中包含y.ttf。 I did not install it on my machine and instead placed it in /assets/fonts. 我没有将其安装在计算机上,而是将其放置在/ assets / fonts中。 I then added the following in my custom.css.scss file. 然后,在我的custom.css.scss文件中添加了以下内容。

@font-face {
  font-family: 'Nokia Pure Headline';    
  src: url('/assets/fonts/y.ttf');
  src: url('/assets/fonts/y.ttf?iefix') format('eot'),
  url('/assets/fonts/y.woff') format('woff'),
  url('/assets/fonts/y.ttf') format('truetype'),
  url('/assets/fonts/y.svg#webfont3AwWkQXK') format('svg');
  font-weight: normal;
  font-style: normal;
}

I then called it in my custom.css.scss stylesheet 然后,在我的custom.css.scss样式表中将其命名

body {
    font-family: y;
}

When I refresh still no success. 当我刷新时仍然没有成功。 Any clues as to why X is distorted, or Y will not work? 关于X为何变形或Y无效的任何线索?

You can use asset pipelines with fonts. 您可以将资产管道与字体一起使用。 First add in application.rb : 首先添加application.rb:

config.assets.paths << Rails.root.join("app", "assets", "fonts")

Rename custom.css.scss file into custom.css.scss.erb and use asset_path for each font file like this : custom.css.scss文件重命名为custom.css.scss.erbasset_path每个字体文件使用asset_path ,如下所示:

@font-face {
  font-family: 'Nokia Pure Headline';    
  src: url('<%= asset_path("y.ttf") %>');
  src: url('<%= asset_path("y.ttf?iefix") %>') format('eot'),
  url('<%= asset_path("y.woff") %>') format('woff'),
  url('<%= asset_path("y.ttf") %>') format('truetype'),
  url('<%= asset_path("y.svg#webfont3AwWkQXK") %>') format('svg');
  font-weight: normal;
  font-style: normal;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM