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.

themes-fonticon.asciidoc 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. ---
  2. title: Font Icons
  3. order: 8
  4. layout: page
  5. ---
  6. [[themes.fonticon]]
  7. = Font Icons
  8. Font icons are icons included in a font. Fonts have many advantages over bitmap
  9. images. Browsers are usually faster in rendering fonts than loading image files.
  10. Web fonts are vector graphics, so they are scalable. As font icons are text
  11. characters, you can define their color in CSS by the regular foreground color
  12. property.
  13. [[themes.fonticon.enabling]]
  14. == Loading Icon Fonts
  15. Vaadin currently comes with one custom icon font: [literal]#++Vaadin Icons++#. It is
  16. automatically included in the Valo theme.
  17. If you use other icon fonts, as described in <<themes.fonticon.custom>>,
  18. you need to load it with a `font` mixin in Sass, as described in
  19. <<themes-fonts#themes.fonts.loading,"Loading Local Fonts">>.
  20. [[themes.fonticon.using]]
  21. == Basic Use
  22. Font icons are resources of type [classname]#FontIcon#, which implements the
  23. [interfacename]#Resource# interface. You can use these special resources for
  24. component icons and such, but not as embedded images, for example.
  25. Each icon has a Unicode codepoint, by which you can use it. Vaadin Framework includes its
  26. own icon font, [literal]#++Vaadin Icons++#, which comes with an enumeration [classname]#VaadinIcons# with all the icons included in the font.
  27. Most typically, you set a component icon as follows:
  28. ////
  29. This code and rest of examples in this file are in uitest/src/main/.../VaadinIconUI.java
  30. ////
  31. [source, Java]
  32. ----
  33. TextField name = new TextField("Name");
  34. name.setIcon(VaadinIcons.USER);
  35. layout.addComponent(name);
  36. // Button allows specifying icon resource in constructor
  37. Button ok = new Button("OK", VaadinIcons.CHECK);
  38. layout.addComponent(ok);
  39. ----
  40. The result is illustrated in <<figure.themes.fonticon.using>>, with the color
  41. styling described next.
  42. [[figure.themes.fonticon.using]]
  43. .Basic Use of Font Icons
  44. image::img/fonticons-basic.png[]
  45. [[themes.fonticon.using.css]]
  46. === Styling the Icons
  47. As font icons are regular text, you can specify their color with the
  48. [literal]#++color++# attribute in CSS to specify the foreground text color. All
  49. HTML elements that display icons in Vaadin have the [literal]#++v-icon++# style
  50. name.
  51. ----
  52. .blueicon .v-icon {
  53. color: blue;
  54. }
  55. ----
  56. If you use the font icon resources in other ways, such as in an
  57. [classname]#Image# component, the style name will be different.
  58. [[themes.fonticon.html]]
  59. == Using Font icons in HTML
  60. You can use font icons in HTML code, such as in a [classname]#Label#, by
  61. generating the HTML to display the icon with the [methodname]#getHtml()# method.
  62. [source, Java]
  63. ----
  64. Label label = new Label("I " +
  65. VaadinIcons.HEART.getHtml() + Vaadin",
  66. ContentMode.HTML);
  67. label.addStyleName("redicon");
  68. ----
  69. The result is illustrated in <<figure.themes.fonticon-html.label>>, with the color
  70. styling described next.
  71. // The ID may not end in ".html"
  72. [[figure.themes.fonticon-html.label]]
  73. .Using Font Icons in Label
  74. image::img/fonticons-html.png[]
  75. You could have set the font color in the label's HTML code as well, or for all
  76. icons in the UI.
  77. You can easily use font icons in HTML code in other ways as well. You just need
  78. to use the correct font family and then use the hex-formatted Unicode codepoint
  79. for the icon. See for example the implementation of the [methodname]#getHtml()#
  80. method in [classname]#FontAwesome#:
  81. ----
  82. @Override
  83. public String getHtml() {
  84. return "<span class=\"v-icon\" style=\"font-family: " +
  85. getFontFamily() + ";\">&#x" +
  86. Integer.toHexString(codepoint) + ";</span>";
  87. }
  88. ----
  89. [[themes.fonticon.anywhere]]
  90. == Using Font Icons in Other Text
  91. You can include a font icon in any text by its Unicode codepoint, which you can
  92. get with the [methodname]#getCodePoint()# method. In such case, however, you
  93. need to use the same font for other text in the same string as well. The
  94. VaadinIcons provided with Valo theme includes a basic character set.
  95. ----
  96. TextField amount = new TextField("Amount (in " +
  97. new String(Character.toChars(
  98. VaadinIcons.DOLLAR.getCodepoint())) +
  99. ")");
  100. amount.addStyleName("amount");
  101. layout.addComponent(amount);
  102. ----
  103. You need to set the font family in CSS.
  104. ----
  105. .v-caption-amount .v-captiontext {
  106. font-family: Vaadin-Icons;
  107. }
  108. ----
  109. [[themes.fonticon.custom]]
  110. == Custom Font Icons
  111. You can easily use glyphs in existing fonts as icons, or create your own.
  112. [[themes.fonticon.custom.creating]]
  113. === Creating New Icon Fonts With IcoMoon
  114. You are free to use any of the many ways to create icons and embed them into
  115. fonts. Here, we give basic instructions for using the
  116. link:http://icomoon.io/app/[IcoMoon] service, where you can pick icons from a
  117. large library of well-designed icons.
  118. After you have selected the icons that you want in your font, you can download
  119. them in a ZIP package. The package contains the icons in multiple formats,
  120. including WOFF, TTF, EOT, and SVG. Not all browsers support any one of them, so
  121. all are needed to support all the common browsers. Extract the [filename]#fonts#
  122. folder from the package to under your theme.
  123. See <<themes-fonts#themes.fonts.loading,"Loading
  124. Local Fonts">> for instructions for loading a custom font.
  125. ifdef::web[]
  126. [[themes.fonticon.custom.implementing]]
  127. === Implementing FontIcon
  128. You can define a font icon for any font available in the browser by implementing
  129. the [interfacename]#FontIcon# interface. The normal pattern for implementing it
  130. is to implement an enumeration for all the symbols available in the font. See
  131. the implementation of [classname]#VaadinIcons# for more details.
  132. You need a FontIcon API for the icons. In the following, we define a font icon
  133. using a normal sans-serif font built-in in the browser.
  134. ----
  135. // Font icon definition with a single symbol
  136. public enum MyFontIcon implements FontIcon {
  137. EURO(0x20AC);
  138. private int codepoint;
  139. MyFontIcon(int codepoint) {
  140. this.codepoint = codepoint;
  141. }
  142. @Override
  143. public String getMIMEType() {
  144. throw new UnsupportedOperationException(
  145. FontIcon.class.getSimpleName()
  146. + " should not be used where a MIME type is needed.");
  147. }
  148. @Override
  149. public String getFontFamily() {
  150. return "sans-serif";
  151. }
  152. @Override
  153. public int getCodepoint() {
  154. return codepoint;
  155. }
  156. @Override
  157. public String getHtml() {
  158. return "<span class=\"v-icon\" style=\"font-family: " +
  159. getFontFamily() + ";\">&#x" +
  160. Integer.toHexString(codepoint) + ";</span>";
  161. }
  162. }
  163. ----
  164. Then you can use it as usual:
  165. ----
  166. TextField name = new TextField("Amount");
  167. name.setIcon(MyFontIcon.EURO);
  168. layout.addComponent(name);
  169. ----
  170. You could make the implementation a class as well, instead of an enumeration, to
  171. allow other ways to specify the icons.
  172. endif::web[]