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

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