You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UsingFontIcons.asciidoc 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. ---
  2. title: Using Font Icons
  3. order: 53
  4. layout: page
  5. ---
  6. [[using-font-icons-in-vaadin-7.2]]
  7. = Using font icons in Vaadin 7.2
  8. A “font icon” is an icon that is a glyph (essentially a character) from
  9. a font. A font that is made for this purpose (containing only icons) is
  10. called an “icon font”. Font icons scale well (being vectors), so you do
  11. not have to make icons for a specific pixel size. Icon fonts are also
  12. typically very light-weight (in bytes), and easy to use once set up.
  13. Vaadin 7.2 comes with generic support for font icons, and has the
  14. popular http://fortawesome.github.io/Font-Awesome/[FontAwesome] icon font
  15. built-in. The support is ‘generic’, in the sense that it does not
  16. include some of the advanced styling options specifically available in
  17. FontAwesome (such as spinning icons). 
  18. FontAwesome is included in the theme by default, but the fonts are only
  19. loaded by the browser when used. If needed, you can also remove the CSS,
  20. minimize the font, or make a custom font.
  21. A demo application showing the result is
  22. https://github.com/Porotype/FontIconDemo[available onGitHub] 
  23. (https://github.com/Porotype/FontIconDemo/tree/master/src/com/example/fonticondemo[java],
  24. https://github.com/Porotype/FontIconDemo/blob/master/WebContent/VAADIN/themes/fonticondemo/fonticondemo.scss[scss])
  25. [[using-fontawesome]]
  26. Using FontAwesome
  27. ~~~~~~~~~~~~~~~~~
  28. You can use font icons with the familiar `setIcon()` method. It looks like
  29. this, when using the built-in FontAwesome:
  30. [source,java]
  31. ....
  32. button.setIcon(FontAwesome.BOOKMARK);
  33. ....
  34. You can also easily embed an icon in any place that allows HTML: 
  35. [source,java]
  36. ....
  37. label.setContentMode(ContentMode.HTML);
  38. label.setValue("Press " + FontAwesome.BOOKMARK.getHtml() + " to bookmark");
  39. ....
  40. [[making-a-custom-set-of-icons]]
  41. Making a custom set of icons
  42. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  43. There are many tools for making icons, and icon fonts. One of our
  44. favorite tools is http://icomoon.io/app[IcoMoon], which is an excellent
  45. online application where you can pick icons from a vast library of
  46. ready-made icon fonts, to compose your own set of icons. When you’re
  47. done, you can download a zip that contains your font files - just copy
  48. the ‘fonts’ folder to your theme.
  49. 1. Browse to http://icomoon.io/app
  50. 2. _Add icons from library_ (the first time, some icons sets are added
  51. automatically)
  52. 3. _Add_ the icon set(s) you fancy (notice the licenses)
  53. 4. Mark the icons you want in your font
  54. 5. _Download_ the font
  55. In IcoMoon you can also produce a customized version of FontAwesome, if
  56. you for instance want to remove unused icons, or swap individual icons.
  57. You can also re-upload your custom icon font, if you want to make
  58. changes or additions to it later. 
  59. 1. _Import icons_
  60. 2. Add/delete/swap icons
  61. 3. _Download_ the new font
  62. When using ready-made icons, please pay attention to the licenses the
  63. different icon sets have.
  64. [[using-a-custom-icon-font]]
  65. Using a custom icon font
  66. ~~~~~~~~~~~~~~~~~~~~~~~~
  67. To use your own icon set, you need to do four things: 
  68. 1. Make an icon font
  69. 2. Make it easily usable from Java and
  70. 3. Load the font in your theme
  71. 4. Use the icons in your application.
  72. (You can skip #1 if you already have an icon font you want to use.)
  73. *1. Compose an icon font* in e.g IcoMoon, download, and copy the “fonts”
  74. folder from the zip to your theme.
  75. *2. Add the following to your styles.scss* OUTSIDE of the `.mytheme{}` block:
  76. [source,scss]
  77. ....
  78. @include font(IcoMoon, '../../fonticondemo/fonts/icomoon');
  79. ....
  80. The first parameter, “IcoMoon”, is the font-family - i.e the name you
  81. want to use for your font. You can choose anything, as long as you use
  82. the same name in Java later. +
  83. The second parameter is the filename base; in this case the files are
  84. called icomoon.ttf, icomoon.svg, etc...
  85. *3. Make use of the icons in Java;* you can make an anonymous FontIcon
  86. instance, but in practice you will probably want to make an enum or some
  87. sort of icon factory. The FontAwesome implementation uses an enum, in
  88. this manner:
  89. [source,java]
  90. ....
  91.   private final int codepoint;
  92.   IcoMoon(int codepoint) {
  93.   this.codepoint = codepoint;
  94.   }
  95.   @Override
  96.   public String getFontFamily() {
  97.   // This must match the name you use in your (S)CSS
  98.   return "IcoMoon";
  99.   }
  100.   @Override
  101.   public int getCodepoint() {
  102.   return codepoint;
  103.   }
  104.   @Override
  105.   public String getHtml() {
  106.   return "<span class=\"v-icon IcoMoon\">&#x"
  107. + Integer.toHexString(codepoint) + ";</span>";
  108.   }
  109.   @Override
  110.   public String getMIMEType() {
  111.   throw new UnsupportedOperationException("Not supported for FontIcon");
  112. }
  113. ....
  114. (Note that you can easily generate the enum from the list of icons in
  115. the zip downloaded from IcoMoon.)
  116. *4. Now you can use your icon:*
  117. [source,java]
  118. ....
  119. button.setIcon(IcoMoon.RIBBON);
  120. ....
  121. [[styling-font-icons]]
  122. Styling font icons
  123. ~~~~~~~~~~~~~~~~~~
  124. You can not generally set style names on the icon itself, instead you
  125. apply styles to the _component_ where the icon is used, much in the same
  126. way you would style anything else in a component.
  127. Given a button with a font icon and a style name:
  128. [source,java]
  129. ....
  130. button.addStyleName("redicon");
  131. button.setIcon(FontAwesome.SAVE);
  132. ....
  133. …you can then style the icon by applying styles to the .v-icon element:
  134. [source,css]
  135. ....
  136. .redicon .v-icon {
  137. color: red;
  138. font-size: 20px;
  139. }
  140. ....
  141. Note that the icon is actually text, so you style it in much the same
  142. way you style text. 
  143. A font icon also gets an additional `.<font-family>` stylename, so you can
  144. apply styles to only font icons (not ‘regular’ image icons):
  145. [source,css]
  146. ....
  147. .v-button .FontAwesome {
  148. color: blue;
  149. }
  150. ....