Using font icons in Vaadin 7.2
A “font icon” is an icon that is a glyph (essentially a character) from a font. A font that is made for this purpose (containing only icons) is called an “icon font”. Font icons scale well (being vectors), so you do not have to make icons for a specific pixel size. Icon fonts are also typically very light-weight (in bytes), and easy to use once set up.
Vaadin 7.2 comes with generic support for font icons, and has the popular FontAwesome icon font built-in. The support is ‘generic’, in the sense that it does not include some of the advanced styling options specifically available in FontAwesome (such as spinning icons).
FontAwesome is included in the theme by default, but the fonts are only loaded by the browser when used. If needed, you can also remove the CSS, minimize the font, or make a custom font.
A demo application showing the result is available onGitHub (java, scss)
Using FontAwesome
You can use font icons with the familiar setIcon()
method. It looks like
this, when using the built-in FontAwesome:
button.setIcon(FontAwesome.BOOKMARK);
You can also easily embed an icon in any place that allows HTML:
label.setContentMode(ContentMode.HTML);
label.setValue("Press " + FontAwesome.BOOKMARK.getHtml() + " to bookmark");
Making a custom set of icons
There are many tools for making icons, and icon fonts. One of our favorite tools is IcoMoon, which is an excellent online application where you can pick icons from a vast library of ready-made icon fonts, to compose your own set of icons. When you’re done, you can download a zip that contains your font files - just copy the ‘fonts’ folder to your theme.
-
Browse to https://icomoon.io/app
-
Add icons from library (the first time, some icons sets are added automatically)
-
Add the icon set(s) you fancy (notice the licenses)
-
Mark the icons you want in your font
-
Download the font
In IcoMoon you can also produce a customized version of FontAwesome, if you for instance want to remove unused icons, or swap individual icons. You can also re-upload your custom icon font, if you want to make changes or additions to it later.
-
Import icons
-
Add/delete/swap icons
-
Download the new font
When using ready-made icons, please pay attention to the licenses the different icon sets have.
Using a custom icon font
To use your own icon set, you need to do four things:
-
Make an icon font
-
Make it easily usable from Java and
-
Load the font in your theme
-
Use the icons in your application.
(You can skip #1 if you already have an icon font you want to use.)
1. Compose an icon font in e.g IcoMoon, download, and copy the “fonts” folder from the zip to your theme.
2. Add the following to your styles.scss OUTSIDE of the .mytheme{}
block:
@include font(IcoMoon, '../../fonticondemo/fonts/icomoon');
The first parameter, “IcoMoon”, is the font-family - i.e the name you
want to use for your font. You can choose anything, as long as you use
the same name in Java later.
The second parameter is the filename base; in this case the files are
called icomoon.ttf, icomoon.svg, etc…
3. Make use of the icons in Java; you can make an anonymous FontIcon instance, but in practice you will probably want to make an enum or some sort of icon factory. The FontAwesome implementation uses an enum, in this manner:
private final int codepoint;
IcoMoon(int codepoint) {
this.codepoint = codepoint;
}
@Override
public String getFontFamily() {
// This must match the name you use in your (S)CSS
return "IcoMoon";
}
@Override
public int getCodepoint() {
return codepoint;
}
@Override
public String getHtml() {
return "<span class=\"v-icon IcoMoon\">&#x"
+ Integer.toHexString(codepoint) + ";</span>";
}
@Override
public String getMIMEType() {
throw new UnsupportedOperationException("Not supported for FontIcon");
}
(Note that you can easily generate the enum from the list of icons in the zip downloaded from IcoMoon.)
4. Now you can use your icon:
button.setIcon(IcoMoon.RIBBON);
Styling font icons
You can not generally set style names on the icon itself, instead you apply styles to the component where the icon is used, much in the same way you would style anything else in a component.
Given a button with a font icon and a style name:
button.addStyleName("redicon");
button.setIcon(FontAwesome.SAVE);
…you can then style the icon by applying styles to the .v-icon element:
.redicon .v-icon {
color: red;
font-size: 20px;
}
Note that the icon is actually text, so you style it in much the same way you style text.
A font icon also gets an additional .<font-family>
stylename, so you can
apply styles to only font icons (not ‘regular’ image icons):
.v-button .FontAwesome {
color: blue;
}