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.

components-label.asciidoc 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. ---
  2. title: Label
  3. order: 7
  4. layout: page
  5. ---
  6. [[components.label]]
  7. = Label
  8. ifdef::web[]
  9. [.sampler]
  10. image:{live-demo-image}[alt="Live Demo", link="http://demo.vaadin.com/sampler/#ui/data-presentation/label"]
  11. endif::web[]
  12. [classname]#Label# component displays non-editable text. This text can be used
  13. for short simple labels or for displaying long text, such as paragraphs. The
  14. text can be formatted in HTML or as preformatted text, depending on the
  15. __content mode__ of the label.
  16. You can give the label text most conveniently in the constructor, as is done in
  17. the following. Label has undefined default width, so it will only take the space it needs.
  18. [source, java]
  19. ----
  20. // A container that is 100% wide by default
  21. VerticalLayout layout = new VerticalLayout();
  22. // label will only take the space it needs
  23. Label label = new Label("Labeling can be dangerous");
  24. layout.addComponent(label);
  25. ----
  26. You can get and set the text of a [classname]#Label# with the
  27. [methodname]#getValue()# and [methodname]#setValue()# methods.
  28. [source, java]
  29. ----
  30. // Get the label's text to initialize a field
  31. TextField editor = new TextField(null, // No caption
  32. label.getValue());
  33. // Change the label's text
  34. editor.addValueChangeListener(event -> label.setValue(event.getValue()));
  35. ----
  36. Even though [classname]#Label# is text and is often used as a caption, it is a
  37. normal component and therefore also has a caption that you can set with
  38. [methodname]#setCaption()#. As with most other components, the caption is
  39. managed by the containing layout.
  40. [[components.label.wrap]]
  41. == Text Width and Wrapping
  42. [classname]#Label# has undefined default width, so it will only take the space it needs.
  43. If the width of the label's text exceeds the available width for the label in the layout,
  44. the text overflows, and normally, gets truncated.
  45. [source, java]
  46. ----
  47. // A container with a defined width.
  48. Panel panel = new Panel("Panel Containing a Label");
  49. panel.setWidth("300px");
  50. panel.setContent(
  51. new Label("This is a Label inside a Panel. There is " +
  52. "enough text in the label to make the text " +
  53. "get truncated when it exceeds the width of the panel."));
  54. ----
  55. As the size of the [classname]#Panel# in the above example is fixed and the
  56. width of [classname]#Label# is the default undefined, the [classname]#Label#
  57. will overflow the layout horizontally and be truncated.
  58. //<<figure.components.label>>.
  59. ////
  60. // TODO update figure to match new label settings in Vaadin Framwork 8
  61. [[figure.components.label]]
  62. .The Label Component
  63. image::img/label-example1.png[width=50%, scaledwidth=75%]
  64. ////
  65. Setting [classname]#Label# to defined width will cause it to not overflow the layout,
  66. but to wrap to the next line.
  67. [[components.label.content-mode]]
  68. == Content Mode
  69. The content of a label is formatted depending on a __content mode__. By default,
  70. the text is assumed to be plain text and any contained HTML-specific characters
  71. will be quoted appropriately to allow rendering the contents of a label in HTML
  72. in a web browser. The content mode can be set in the constructor or with
  73. [methodname]#setContentMode()#, and can have the values defined in the
  74. [classname]#ContentMode# enumeration type in
  75. [package]#com.vaadin.shared.ui# package:
  76. TEXT:: The default content mode where the label contains only plain text. All
  77. characters are allowed, including the special [literal]#++<++#,
  78. [literal]#++>++#, and [literal]#++&++# characters in HTML, which are
  79. quoted properly in HTML while rendering the component. This is the default mode.
  80. PREFORMATTED:: Content mode where the label contains preformatted text. It will be, by default,
  81. rendered with a fixed-width typewriter font. Preformatted text can contain line
  82. breaks, written in Java with the [literal]#++\n++# escape sequence for a newline
  83. character (ASCII 0x0a), or tabulator characters written with [literal]#++\t++#
  84. (ASCII 0x09).
  85. HTML:: Content mode where the label contains HTML.
  86. +
  87. Please note the following security and validity warnings regarding the HTML
  88. content mode.
  89. [WARNING]
  90. .Cross-Site Scripting Warning
  91. ====
  92. Having [classname]#Label# in HTML content mode allows pure HTML content. If the
  93. content comes from user input, you should always carefully sanitize it to
  94. prevent cross-site scripting (XSS) attacks. Please see
  95. <<../advanced/advanced-security#advanced.security.sanitizing,"Sanitizing
  96. User Input to Prevent Cross-Site Scripting">>.
  97. Also, the validity of the HTML content is not checked when rendering the
  98. component and any errors can result in an error in the browser. If the content
  99. comes from an uncertain source, you should always validate it before displaying
  100. it in the component.
  101. ====
  102. The following example demonstrates the use of [classname]#Label# in different
  103. modes.
  104. [source, java]
  105. ----
  106. Label textLabel = new Label(
  107. "Text where formatting characters, such as \\n, " +
  108. "and HTML, such as <b>here</b>, are quoted.",
  109. ContentMode.TEXT);
  110. Label preLabel = new Label(
  111. "Preformatted text is shown in an HTML <pre> tag.\n" +
  112. "Formatting such as\n" +
  113. " * newlines\n" +
  114. " * whitespace\n" +
  115. "and such are preserved. HTML tags, \n"+
  116. "such as <b>bold</b>, are quoted.",
  117. ContentMode.PREFORMATTED);
  118. Label htmlLabel = new Label(
  119. "In HTML mode, all HTML formatting tags, such as \n" +
  120. "<ul>"+
  121. " <li><b>bold</b></li>"+
  122. " <li>itemized lists</li>"+
  123. " <li>etc.</li>"+
  124. "</ul> "+
  125. "are preserved.",
  126. ContentMode.HTML);
  127. ----
  128. The rendering will look as shown in <<figure.components.label.content-mode>>.
  129. [[figure.components.label.content-mode]]
  130. .Label Content Modes
  131. image::img/label-modes.png[width=75%, scaledwidth=100%]
  132. ifdef::web[]
  133. [[components.label.spacing]]
  134. == Spacing with a [classname]#Label#
  135. You can use a [classname]#Label# to create vertical or horizontal space in a
  136. layout. If you need a empty "line" in a vertical layout, having just a label
  137. with empty text is not enough, as it will collapse to zero height. The same goes
  138. for a label with only whitespace as the label text. You need to use a
  139. non-breaking space character, either [literal]#++&nbsp;++# or
  140. [literal]#++&#160;++#:
  141. [source, java]
  142. ----
  143. layout.addComponent(new Label("&nbsp;", ContentMode.HTML));
  144. ----
  145. Using the [parameter]#ContentMode.PREFORMATTED# mode has the same effect;
  146. preformatted spaces do not collapse in a vertical layout. In a
  147. [classname]#HorizontalLayout#, the width of a space character may be
  148. unpredictable if the label font is proportional, so you can use the preformatted
  149. mode to add em-width wide spaces.
  150. If you want a gap that has adjustable width or height, you can use an empty
  151. label if you specify a height or width for it. For example, to create vertical
  152. space in a [classname]#VerticalLayout#:
  153. [source, java]
  154. ----
  155. Label gap = new Label();
  156. gap.setHeight("1em");
  157. verticalLayout.addComponent(gap);
  158. ----
  159. You can make a flexible expanding spacer by having a relatively sized empty
  160. label with [literal]#++100%++# height or width and setting the label as
  161. expanding in the layout.
  162. [source, java]
  163. ----
  164. // A wide component bar
  165. HorizontalLayout horizontal = new HorizontalLayout();
  166. horizontal.setWidth("100%");
  167. // Have a component before the gap (a collapsing cell)
  168. Button button1 = new Button("I'm on the left");
  169. horizontal.addComponent(button1);
  170. // An expanding gap spacer
  171. Label expandingGap = new Label();
  172. expandingGap.setWidth("100%");
  173. horizontal.addComponent(expandingGap);
  174. horizontal.setExpandRatio(expandingGap, 1.0f);
  175. // A component after the gap (a collapsing cell)
  176. Button button2 = new Button("I'm on the right");
  177. horizontal.addComponent(button2);
  178. ----
  179. endif::web[]
  180. [[components.label.css]]
  181. == CSS Style Rules
  182. [source, css]
  183. ----
  184. .v-label { }
  185. pre { } /* In PREFORMATTED content mode */
  186. ----
  187. The [classname]#Label# component has a [literal]#++v-label++# overall style. In
  188. the [parameter]#PREFORMATTED# content mode, the text is wrapped inside a
  189. [literal]#++<pre>++# element.