Rails使用图标库技巧

网页中经常需要使用一些小图标,这里我们想使用Font Awesome这个图标库里的图标,我们可以到rubygems.org查看是否有人将该图标库封装成gem使得我们使用更加敏捷

在rubugems搜索结果如下:

然后在结果页面点击"源代码":

来到源代码的github页面,我们按照Readme来操作:

然后执行命令bundle 进行安装(执行bundle install也一样)

安装完gem要重启服务器

?

要引用该gem还得在application.css中添加引用如下:

如果不是application.css而是application.css.scss文件,那么就不是*= require font-awesome,而是如下
				

如果是application.css.sass文件,那么就是如下
比起scss,区别是没有双引号和结尾的分号
				
[email protected]
						font-awesome

接下来我们在视图页面中使用:

首先在官网查找所需图标:

然后点击进入即可查看使用的代码:

把这部分代码复制到视图中即可(因为我们安装了gem所以本地图标库就包含官网的这些图标,我们直接加这个代码就可以使用该图标了)

如上图标显示效果不理想,我们可以给它添加自己的样式icon-muted如下:

?

附带github的这个gem的Readme文档,对比阅读:

?README.md

font-awesome-rails

??

font-awesome-rails provides the?Font-Awesome?web fonts and stylesheets as a Rails engine for use with the asset pipeline.

Installation

Add this to your Gemfile:

gem
"font-awesome-rails"

and run?bundle install.

Usage

In your?application.css, include the css file:

/*

*= require font-awesome

*/

Then restart your webserver if it was previously running.

Congrats! You now have scalable vector icon support. Pick an icon and check out the?FontAwesome Examples.

Sass Support

If you prefer?SCSS, add this to your?application.css.scss?file:

@import "font-awesome";

If you use the?Sass indented syntax, add this to your?application.css.sass?file:

@import
font-awesome

Helpers

There are also some helpers (fa_icon?and?fa_stacked_icon) that make your views?icontastic!

fa_icon "camera-retro"

# => <i class="fa fa-camera-retro"></i>

?

fa_icon "camera-retro", text:
"Take a photo"

# => <i class="fa fa-camera-retro"></i> Take a photo

?

fa_icon "chevron-right", text:
"Get started", right:
true

# => Get started <i class="fa fa-chevron-right"></i>

?

fa_icon "quote-left 4x", class:
"text-muted pull-left"

# => <i class="fa fa-quote-left fa-4x text-muted pull-left"></i>

?

content_tag(:li, fa_icon("check li", text:
"Bulleted list item"))

# => <li><i class="fa fa-check fa-li"></i> Bulleted list item</li>

fa_stacked_icon "twitter", base:
"square-o"

# => <span class="fa-stack">

# => <i class="fa fa-square-o fa-stack-2x"></i>

# => <i class="fa fa-twitter fa-stack-1x"></i>

# => </span>

?

fa_stacked_icon "dollar inverse", base:
"circle", class:
"fa-5x"

# => <span class="fa-stack fa-5x">

# => <i class="fa fa-circle fa-stack-2x"></i>

# => <i class="fa fa-dollar fa-inverse fa-stack-1x"></i>

# => </span>

?

fa_stacked_icon "terminal inverse", base:
"square", class:
"pull-right", text:
"Hi!"

# => <span class="fa-stack pull-right">

# => <i class="fa fa-square fa-stack-2x"></i>

# => <i class="fa fa-terminal fa-inverse fa-stack-1x"></i>

# => </span> Hi!

Misc

Rails engines

When building a Rails engine that includes font-awesome-rails as a dependency, be sure to?require "font-awesome-rails"somewhere during the intialization of your engine. Otherwise, Rails will not automatically pick up the load path of the font-awesome-rails assets and helpers (source 1,?source 2,?source 3).

Deploying to sub-folders

It is sometimes the case that deploying a Rails application to a production environment requires the application to be hosted at a sub-folder on the server. This may be the case, for example, if Apache HTTPD or Nginx is being used as a front-end proxy server, with Rails handling only requests that come in to a sub-folder such as?http://example.com/myrailsapp. In this case, the FontAwesome gem (and other asset-serving engines) needs to know the sub-folder, otherwise you can experience a problem roughly described as?"my app works fine in development, but fails when I deploy it".

To fix this, set the?relative URL root?for the application. In the environment file for the deployed version of the app, for exampleconfig/environments/production.rb, set the config option?action_controller.relative_url_root:

MyApp::Application.configure do

...

?

# set the relative root, because we‘re deploying to /myrailsapp

config.action_controller.relative_url_root = "/myrailsapp"

?

...

end

The default value of this variable is taken from?ENV[‘RAILS_RELATIVE_URL_ROOT‘], so configuring the environment to defineRAILS_RELATIVE_URL_ROOT?is an alternative strategy.

Rails 3.2

Note:?In Rails 3.2, make sure font-awesome-rails is outside the bundler asset group so that these helpers are automatically loaded in production environments.

Versioning

Versioning follows the core releases of Font-Awesome which follows Semantic Versioning 2.0 as defined at?http://semver.org. We will do our best not to make any breaking changes until Font-Awesome core makes a major version bump.

License

?

?

时间: 2024-08-06 04:44:47

Rails使用图标库技巧的相关文章

阿里UX矢量图标库–最强大的矢量图标库(Icon font制作力荐工具)

继前面介绍过ICON-FONT的制作后,找了几个ICON库都是国外的今天偶然发现阿里巴巴的图标矢量库,www.iconfont.cn用了之后感觉很强大,丰富的图标库(集合阿里妈妈&淘宝的图标库),还支持自己上传,关键还能生成你项目专用的CDN,速度不言而喻,支持国产.下面引用介绍 转载请注明来源:Web前端(W3Cways.com) - Web前端学习之路 » 阿里UX矢量图标库–最强大的矢量图标库(Icon font制作力荐工具) 使用SVG来代替Web Iconfont(图形字体化) 从最开

基于HTML5的开源图标库-ECharts

ECharts: 来自百度商业前端数据可视化团队,基于html5 Canvas,纯Javascript图表库,底层依赖ZRender,商业产品常用图表库,提供直观,生动,可交互,可个性化定制的数据可视化图表.创新的拖拽重计算.数据视图.值域漫游等特性大大增强了用户体验,赋予了用户对数据进行挖掘.整合的能力.图表类型支持折线图(区域图).柱状图(条状图).散点图(气泡图).K线图.饼图(环形图).雷达图(填充雷达图).和弦图.力导向布局图.地图,同时支持任意维度的堆积和多图表混合展现. 链接是:h

《Ext.net》处女作,本人也是处女座。吼吼(内容包括EXT自带图标库文件和Font Awesome图标库),笔记特别详细,适合刚刚自学的朋友。

完全处于自学啊,压力,不过好在有个asp.net的师傅.不过都数都是自己做,自己百度.程序员就应该多找度娘.能不麻烦别人就不要麻烦别人. 我自学的方式是:参考官网API.然后英文看不懂用有道翻译.然后多练习,结合项目一起做.最后才去问师傅. 官网API地址:http://examples.ext.net/ 参考学习资料地址:http://www.qeefee.com/article/extnet-learn-01-using-extnet-in-webform 这里面很详细,相信慢慢来一定能成为

2017.4.7 图标库Font Awesome的使用

1.Font Awesome 是一个非常方便的图标库.这些图标都是矢量图形,被保存在 .svg 的文件格式中.这些图标就和字体一样,你可以通过像素单位指定它们的大小,它们将会继承其父HTML元素的字体大小. 2.你可以将 Font Awesome 图标库增添至任何一个应用中,方法很简单,只需要在你的 HTML 头部增加下列代码即可: <link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/4.2.0/css

如何合并两个图标库

http://blog.csdn.net/crystal6918/article/details/52994393 最近在项目中碰到了要合并两个icon库的需求:我们项目本来用的是bootstrap提供的glyphicons,但是现在需要用一个别的库的icon,想把这两个库合并在一起供项目使用,研究了一番后整理个教程吧~ 利用https://icomoon.io/app/#/select 这一工具进行这两种图标库的合并,操作步骤如下: 导入图标库 点击页面左上角的import icons,分别导

字体图标库使用方法

一些字体图标库的网站 https://icomoon.io/app/#/selecthttp://www.iconfont.cn/http://www.bootcss.com/ http://www.bootcss.com/p/cikonss/ 以第一个链接网站里的字体为例 选择自己需要图标,点击右下角下载下来 继续点击右下角--点击下载 然后下载的文件一般都是这几个基本部件 把fonts文件夹复制到自己的项目中根目录中 打开css文件 把代码复制到自己的某个css文件中,然后html引用,重点

Font Awesome 图标库

Font Awesome 是一个非常方便的图标库.这些图片都是矢量图,以 .svg 文件格式保存.这些图标用起来就像字体一样.你可以使用像素单位来指定他们的大小,它们会继承父级HTML元素的字体大小. 有很过可爱的小图标 <div class="row"> <div class="col-xs-4"> <button class="btn btn-block btn-primary"><i class=&

php大力力 [037节] Iconfont-阿里巴巴矢量图标库

Iconfont-阿里巴巴矢量图标库 从此不求人:自主研发一套PHP前端开发框架 Iconfont-中国第一个最大且功能最全的矢量图标库,提供矢量图标下载.在线存储.格式转换等功能.阿里巴巴体验团队倾力打造,设计和前端开发的便捷工具 www.iconfont.cn/ 从300到300万,一个远程外包团队的发展历程和经验 PHP检测url地址是否被百度收录 thinkphp实现excel数据的导入导出(修改适用3.2.3)H5编辑后台 thinkphp实现excel数据的导入导出(修改适用3.2.

一个标签的72变,打造一个纯CSS图标库

每次要用到图标的时候都会到 icono 去copypaste,但每次用到的时候尺寸都各不一样,总是要调整参数,巨烦.当然你可以会想到用zoom.scale来做缩放,但是这样的缩放会使得线宽也变粗了,不甚满意. 终于下定心思来改造一个可缩放的图标库.github先上:https://github.com/qieguo2016/iconoo,目前提供下载link标签引入和npm+webpack的引入方式,详见项目的readme.(喂,求star!) 关于改造,一开始的想法就是使用百分比尺寸来改造,然