Font Icons

Font icons are icons included in a font. Fonts have many advantages over bitmap images. Browsers are usually faster in rendering fonts than loading image files. Web fonts are vector graphics, so they are scalable. As font icons are text characters, you can define their color in CSS by the regular foreground color property.

Loading Icon Fonts

Vaadin currently comes with one custom icon font: FontAwesome. It is automatically enabled in the Valo theme. For other themes, you need to include it with the following line in your project theme, after importing the base theme:

  1. @include fonticons;

If you use other icon fonts, as described in Custom Font Icons, and the font is not loaded by a base theme, you need to load it with a font mixin in Sass, as described in “Loading Local Fonts”.

Basic Use

Font icons are resources of type FontIcon, which implements the Resource interface. You can use these special resources for component icons and such, but not as embedded images, for example.

Each icon has a Unicode codepoint, by which you can use it. Vaadin includes an awesome icon font, FontAwesome, which comes with an enumeration of all the icons included in the font.

Most typically, you set a component icon as follows:

Java

  1. TextField name = new TextField("Name");
  2. name.setIcon(FontAwesome.USER);
  3. layout.addComponent(name);
  4. // Button allows specifying icon resource in constructor
  5. Button ok = new Button("OK", FontAwesome.CHECK);
  6. layout.addComponent(ok);

See the on-line example.

The result is illustrated in Basic Use of Font Icons, with the color styling described next.

fonticons basic

Basic Use of Font Icons

Styling the Icons

As font icons are regular text, you can specify their color with the color attribute in CSS to specify the foreground text color. All HTML elements that display icons in Vaadin have the v-icon style name.

  1. .v-icon {
  2. color: blue;
  3. }

If you use the font icon resources in other ways, such as in an Image component, the style name will be different.

Using Font icons in HTML

You can use font icons in HTML code, such as in a Label, by generating the HTML to display the icon with the getHtml() method.

Java

  1. Label label = new Label("I " +
  2. FontAwesome.HEART.getHtml() + " Vaadin",
  3. ContentMode.HTML);
  4. label.addStyleName("redicon");
  5. layout.addComponent(label);

See the on-line example.

The HTML code has the v-icon style, which you can modify in CSS:

CSS

  1. .redicon .v-icon {
  2. color: red;
  3. }

See the on-line example.

The result is illustrated in Using Font Icons in Label, with the color styling described next.

fonticons html

Using Font Icons in Label

You could have set the font color in the label’s HTML code as well, or for all icons in the UI.

You can easily use font icons in HTML code in other ways as well. You just need to use the correct font family and then use the hex-formatted Unicode codepoint for the icon. See for example the implementation of the getHtml() method in FontAwesome:

  1. @Override
  2. public String getHtml() {
  3. return "<span class=\"v-icon\" style=\"font-family: " +
  4. getFontFamily() + ";\">&#x" +
  5. Integer.toHexString(codepoint) + ";</span>";
  6. }

See the on-line example.

Using Font Icons in Other Text

You can include a font icon in any text by its Unicode codepoint, which you can get with the getCodePoint() method. In such case, however, you need to use the same font for other text in the same string as well. The FontAwesome provided in Vaadin includes a basic character set.

  1. TextField amount = new TextField("Amount (in " +
  2. new String(Character.toChars(
  3. FontAwesome.BTC.getCodepoint())) +
  4. ")");
  5. amount.addStyleName("awesomecaption");
  6. layout.addComponent(amount);

See the on-line example.

You need to set the font family in CSS.

  1. .v-caption-awesomecaption .v-captiontext {
  2. font-family: FontAwesome;
  3. }

See the on-line example.

Custom Font Icons

You can easily use glyphs in existing fonts as icons, or create your own.

Creating New Icon Fonts With IcoMoon

You are free to use any of the many ways to create icons and embed them into fonts. Here, we give basic instructions for using the IcoMoon service, where you can pick icons from a large library of well-designed icons.

Font Awesome is included in IcoMoon’s selection of icon libraries. Note that the codepoints of the icons are not fixed, so the FontAwesome enum is not compatible with such custom icon fonts.

After you have selected the icons that you want in your font, you can download them in a ZIP package. The package contains the icons in multiple formats, including WOFF, TTF, EOT, and SVG. Not all browsers support any one of them, so all are needed to support all the common browsers. Extract the fonts folder from the package to under your theme.

See “Loading Local Fonts” for instructions for loading a custom font.

Implementing FontIcon

You can define a font icon for any font available in the browser by implementing the FontIcon interface. The normal pattern for implementing it is to implement an enumeration for all the symbols available in the font. See the implementation of FontAwesome for more details.

You need a FontIcon API for the icons. In the following, we define a font icon using a normal sans-serif font built-in in the browser.

  1. // Font icon definition with a single symbol
  2. public enum MyFontIcon implements FontIcon {
  3. EURO(0x20AC);
  4. private int codepoint;
  5. MyFontIcon(int codepoint) {
  6. this.codepoint = codepoint;
  7. }
  8. @Override
  9. public String getMIMEType() {
  10. throw new UnsupportedOperationException(
  11. FontIcon.class.getSimpleName()
  12. + " should not be used where a MIME type is needed.");
  13. }
  14. @Override
  15. public String getFontFamily() {
  16. return "sans-serif";
  17. }
  18. @Override
  19. public int getCodepoint() {
  20. return codepoint;
  21. }
  22. @Override
  23. public String getHtml() {
  24. return "<span class=\"v-icon\" style=\"font-family: " +
  25. getFontFamily() + ";\">&#x" +
  26. Integer.toHexString(codepoint) + ";</span>";
  27. }
  28. }

See the on-line example.

Then you can use it as usual:

  1. TextField name = new TextField("Amount");
  2. name.setIcon(MyFontIcon.EURO);
  3. layout.addComponent(name);

See the on-line example.

You could make the implementation a class as well, instead of an enumeration, to allow other ways to specify the icons.