If you’ve used any @font-face icon fonts, whether with twitter-boostrap or Zurb, or elsewhere, you know they can be handy. Its nice from a rails perspective to have them bundled up in an engine, and its nice to have an easy-to-remember name to find them with.
Its relatively easy to find free or low-cost icon fonts that you can use with @font-face. The problem is that they might not be Rails ready, and there might be a few hundred in a font, when you only need 10. If you’d like to be picky about which icons you include in your app, or perhaps you want different icons from different font sets, follow along.
There are lots of ways to get a set of icon fonts. I’m choosing to use Fontello.
So, to start off, go to Fontello and create the set of icons that you want to use. Download and unzip.
We’ll be modifying the instructions for gemifying assets that can be found in Derek Prior’s blog at prioritized.net.
- Use Bundler to create the gem
- Edit lib/myfonts.rb and add:
- We’re going to add everything we need to the vendor directory.
The fontello download contains a font directory with several fonts in it. Copy the contents of that directory to the fonts directory you just created.
Edit the .gemspec file and change gem.files to:
- Then add:
- You should also edit the gem description and summary to their proper values. Here is my gemspec as a sample result:
Copy the fontello.css file from the fontello download to vendor/assets/stylesheets. I haven’t needed the other css files.
Rename the fontello.css file
- You will need to edit the src: lines in the css file to look for the fonts in assets. I count 5 places to edit in my demo.
- If you would like a convenient way to change the colors of the icons, add something like this at the end of the css file:
- The above will allow you to use html such as:
- You’ll add this to your rails app as you normally would any other gem. I would recommend not pushing to rubygems.org, and just serving the gem from github or bitbucket or your own gem server. Use this example, changing the url to your own, unless you want to use my 4 emoticons:
- You should then edit your app/assets/stylesheets/application.css file using this example, remembering that its the name of the css file, sans extension, that you should use.
- You can then use html like this:
That’s about it. If you would like to see my demos, they can be found here:
Rails App: https://github.com/dmoulton/myfonts-demo-app