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)

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?