Sna*_*ops 63 fonts asset-pipeline ruby-on-rails-4
我有一个Rails 4应用程序,我正在尝试使用自定义字体.
我已经关注了很多关于这方面的教程,不知何故它只是不适用于我的应用程序.
我正在使用application.css.less
并有以下声明:
@font-face {
font-family: 'HDVPeace';
src: font-url('HDV_Peace.eot');
src: font-url('HDV_Peace.eot?iefix') format('eot'),
font-url('HDV_Peace.woff') format('woff'),
font-url('HDV_Peace.ttf') format('truetype'),
font-url('HDV_Peace.svg#webfont') format('svg');
}
Run Code Online (Sandbox Code Playgroud)
注意:我尝试过使用 url()
而不是 font-url()
.前者在控制台上生成404错误,后者似乎根本不做任何事情.在资源下的chrome dev工具中,字体文件不会出现在 文件夹下assets
或任何位置
在我的config/application.rb
身上:
config.assets.paths << Rails.root.join('app', 'assets', 'fonts')
Run Code Online (Sandbox Code Playgroud)
在我config/environments/development.rb
和config/environments/production.rb
我都有:
config.assets.paths << Rails.root.join('app', 'assets', 'fonts')
config.assets.precompile += %w( .svg .eot .woff .ttf)
Run Code Online (Sandbox Code Playgroud)
我的字体文件位于app/assets/fonts
以下文件夹中,并且不包含在...
我错过了什么?
更新:
文件夹结构
app
|----assets
|----fonts
|----HDV_Peace.eot
|----HDV_Peace.svg
|----HDV_Peace.ttf
|----HDV_Peace.woff
Run Code Online (Sandbox Code Playgroud)
Par*_*ert 60
您的@font-face
声明非常接近,您只是错过/assets
了网址声明中的前缀.
@font-face {
font-family: 'HDVPeace';
src: url('/assets/HDV_Peace.eot');
src: url('/assets/HDV_Peace.eot?iefix') format('eot'),
url('/assets/HDV_Peace.woff') format('woff'),
url('/assets/HDV_Peace.ttf') format('truetype'),
url('/assets/HDV_Peace.svg#webfont') format('svg');
}
Run Code Online (Sandbox Code Playgroud)
添加的任何内容assets.paths
都可直接在/assets
开发和生产的路径下使用.您只需要内部的资产路径修改行application.rb
,再次执行它development.rb
并且production.rb
只是冗余.
此外,所有字体格式基本上都是二进制的.无需预编译它们,因此您可以安全地删除assets.precompile
添加内容.
这是一个新的Rails 4应用程序,提供自定义字体.除了css中的font-face import语句和添加一个页面以显示没有其他内容已经完成:
https://github.com/sorentwo/font-example
小智 51
在Rails 4中,有一个帮助器来设置字体的路径.
如果你有/ assets/fonts或vendor/assets/fonts中的字体,Rails 4会找到它们!要利用这一点,在Bootstrap CSS文件中将@font_face调用更改为
@font-face {
font-family: 'Glyphicons Halflings';
src: font-url('glyphicons-halflings-regular.eot');
src: font-url('glyphicons-halflings-regular.eot?#iefix') format('embedded-opentype'),
font-url('glyphicons-halflings-regular.woff') format('woff'),
font-url('glyphicons-halflings-regular.ttf') format('truetype'),
font-url('glyphicons-halflings-regular.svg#glyphicons_halflingsregular') format('svg');
}
Run Code Online (Sandbox Code Playgroud)
请注意,字体文件前面没有文件夹规范.这是由铁轨助手完成的.
Mik*_*any 20
请勿对您的字体目录进行硬编码,因为该位置是动态的.
就像有图像的内置助手一样,还有内置的字体助手:http: //api.rubyonrails.org/classes/ActionView/Helpers/AssetUrlHelper.html#method-i-font_url
例:
@font-face {
font-family: 'QuicksandOTF';
src: font_url('Quicksand-Regular.otf') format('opentype');
font-weight: normal;
font-style: normal;
}
Run Code Online (Sandbox Code Playgroud)
reb*_*tte 11
这对我来说适用于Rails 4.1应用程序.
@font-face {
font-family: 'icomoon';
src: url(font-path('icomoon.eot') + "?#iefix") format('embedded-opentype'),
url(font-path('icomoon.woff')) format('woff'),
url(font-path('icomoon.ttf')) format('truetype'),
url(font-path('icomoon.svg') + "#icomoon") format('svg');
}
[class^="icon-"], [class*=" icon-"] {
font-family: 'icomoon';
speak: none;
font-style: normal;
font-weight: normal;
font-variant: normal;
text-transform: none;
line-height: 1;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
63458 次 |
最近记录: |