Blog.

Using Font Icons in Ext – FontAwesome

What is a Font Icon?

Font Icon

Every application or operating system needs icons, small images symbolizing concepts or actions that are easier to recognize and locate than written texts. They used to be 16x16 pixels but with increased screen resolutions like retina displays we need much bigger images: 64x64, 256x256 or even higher. The ‘ole good 16x16 just do not look good anymore.

The idea of font icon is simple: fonts are not bitmap images but vector shapes so if we draw a shape and assign it to a character in a font we get scalable and resolution independent icon that looks good at any size. So that’s font icon.

What is an Icon Font?

Sure, one icon is not enough, and a couple of special glyphs in standard fonts as Arial also won’t do designers and developers put together collections of useful shapes, icons into specialized fonts, icon fonts.

If you google for “icon font” you find many of them, they are increasingly popular. Here is the list of a few of them:

I’ll discuss FontAwesome in this post.

FontAwesome

Take a look at all icons at FontAwesome site. There are about 370 icons in 8 categories, the selection that should be enough for a majority of applications.

Each icon has a symbolic name that is CSS class in fact. If you click on an icon, you see the icon details. The most important information is:

  • Unicode number, e.g. f14e for fa-compass

  • HTML markup to show an icon, e.g.

    
    

The stylesheet that contains CSS classes is also provided either for download or hosted on CDN:

 

You only need to include the above stylesheet in the head of the page and then place the markup of the icon wherever you want to show it, like here .

Using FontAwesome in Ext application

The aforementioned way is fine for showing icons in text but in Ext we want icons in headers, buttons, menu items, etc. How to go about that?

Fortunately, it’s very easy. Many components have configuration option glyph that takes unicode of the icon plus the optional font family name. That is enough to show the font icon (besides including the font stylesheet in the head).

Button configuration would then look like this:

Ext.widget('button', {
     text:'Save'
    ,glyph:'xf0c7@FontAwesome'
});

Panel like this:

Ext.widget('panel', {
     title:'Info Panel'
    ,border:true
    ,glyph:'xf05a@FontAwesome'
});

Nobody likes too much typing of font name in every glyph so we would rather call

Ext.setGlyphFontFamily('FontAwesome');

early in ourc app (init or launch method of Application).

The we would then configure our button and panel like this:

Ext.widget('button', {
     text:'Save'
    ,glyph:0xf0c7
});

Ext.widget('panel', {
     title:'Info Panel'
    ,border:true
    ,glyph:0xf05a
});

Important: Mind that the type of glyph in the above code is number, not string. The global glyph font family is ignored if glyph type is string.

saki
Follow me:
Latest posts by saki (see all)

15 Responses

    1. Yes, you can, however, you might need to override or extend TreeColumn because you may need, or it would be more convenient to have, changed HTML markup of nodes. Maybe not different tags but added or changed HTML properties of the existing tags.

      That is how I’ve done the navigation tree at the examples page.

      1. Nevermind, Saki, I just modified the TreeColumn template. The tree is an isolated view where the reuse would stop so that’s enough for now. Thanks!

      2. I’ve extended Ext.tree.Column and I’ve overridden cellTpl to recognize additional fields such as iconColor, etc.

        Then I use columns config option of the tree panel specifying the extension’s xtype.

      1. You can add path to Font Awesome to app.json this way:

            "css": [
                {
                    "path": "bootstrap.css",
                    "bootstrap": true
                },
                {
                    "path": "http://netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css",
                    "bootstrap": true
                }
            ],
        
  1. EXT JS 5.0.1:

    1. Reference the css in index.html file;
    2. Then simply use the iconCls property of an button item, like the following example:

    {
    xtype: ‘button’,
    id: ‘b_grid_edit_id’,
    iconCls: ‘fa fa-pencil fa-lg’,
    text: ‘Edit’
    }

    Hope this helps!

  2. Yes, it can be that something changed in Ext 5. Anyway, I set the glyph family even earlier: at the beginning of app.js before Ext.application call.

    Link updated.

    Cheers.

  3. Thanks for the write up, Saki.

    One note. I could not get this to work by putting setGlyphFontFamily(…) in launch(). It only worked in init(). I was testing this in Ext JS 5.0.1.

    Also, if you’re interested in updating it, the new cdn link for FA is: //maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css

    Thanks again!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Enter your username and password to log into your account. Don't have an account? Sign up.

Want to collaborate on an upcoming project?