Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

themes-fonticon.asciidoc 8.0KB

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