What is a 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
forfa-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
.
- Ext, Angular, React, and Vue - 27. June 2019
- The Site Resurgence - 11. February 2018
- Configuring ViewModel Hierarchy - 19. June 2015
15 Responses
I’m trying to set the font to a node using iconCls on my nodes in 5.0.1 but it’s not working. Can I use fonts for icons on tree nodes?
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.
What method do I override or what method do I overload? I can’t check your source of the examples page…
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!
I’ve extended
Ext.tree.Column
and I’ve overriddencellTpl
to recognize additional fields such asiconColor
, etc.Then I use
columns
config option of the tree panel specifying the extension’s xtype.I don’t understand 🙁
in which folder I must to put the font-awesome-4.2.0.Zip
You do not download font zip file, you only add css file link to your index.html or app.json
Sorry, but I’m kinda new to this. Can you explain how I can link this in app.json
thank you
You can add path to Font Awesome to app.json this way:
How to change the icon color in Ext 5.0.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!
Yes, thanks god it works fine in Ext 5.0.1. But the icon defined this way is misaligned in Ext 5.0.0.
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.
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!