summaryrefslogtreecommitdiffstats
path: root/documentation/articles/UsingFontIcons.asciidoc
blob: 7713f48c7443524eb460c781ec880a3afd0358e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
---
title: Using Font Icons
order: 53
layout: page
---

[[using-font-icons-in-vaadin-7.2]]
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 http://fortawesome.github.io/Font-Awesome/[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
https://github.com/Porotype/FontIconDemo[available onGitHub] 
(https://github.com/Porotype/FontIconDemo/tree/master/src/com/example/fonticondemo[java],
https://github.com/Porotype/FontIconDemo/blob/master/WebContent/VAADIN/themes/fonticondemo/fonticondemo.scss[scss])

[[using-fontawesome]]
Using FontAwesome
~~~~~~~~~~~~~~~~~

You can use font icons with the familiar `setIcon()` method. It looks like
this, when using the built-in FontAwesome:

[source,java]
....
button.setIcon(FontAwesome.BOOKMARK);
....

You can also easily embed an icon in any place that allows HTML: 

[source,java]
....
label.setContentMode(ContentMode.HTML);
label.setValue("Press " + FontAwesome.BOOKMARK.getHtml() + " to bookmark");
....

[[making-a-custom-set-of-icons]]
Making a custom set of icons
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

There are many tools for making icons, and icon fonts. One of our
favorite tools is http://icomoon.io/app[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.

1.  Browse to http://icomoon.io/app
2.  _Add icons from library_ (the first time, some icons sets are added
automatically)
3.  _Add_ the icon set(s) you fancy (notice the licenses)
4.  Mark the icons you want in your font
5.  _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. 

1.  _Import icons_
2.  Add/delete/swap icons
3.  _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]]
Using a custom icon font
~~~~~~~~~~~~~~~~~~~~~~~~

To use your own icon set, you need to do four things: 

1.  Make an icon font
2.  Make it easily usable from Java and
3.  Load the font in your theme
4.  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:

[source,scss]
....
@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:

[source,java]
....
  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:*

[source,java]
....
  button.setIcon(IcoMoon.RIBBON);
....

[[styling-font-icons]]
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:

[source,java]
....
  button.addStyleName("redicon");
  button.setIcon(FontAwesome.SAVE);
....

…you can then style the icon by applying styles to the .v-icon element:

[source,css]
....
  .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):

[source,css]
....
.v-button .FontAwesome {
  color: blue;
}
....