3.5.2.1.22. Label
Label
is a text component that displays static text or value of an entity attribute.
XML name of the component: label
Below is an example of setting a label with text taken from the localized message pack:
<label value="msg://orders"/>
The value
attribute sets text for a label.
In a web client, the text contained in value
will be split into multiple lines if its length exceeds the width value. Therefore, to display a multiline label, it is sufficient to specify an absolute value of width. If the label text is too long and the value of width is not specified, the text will be truncated.
<label value="Label, which should be split into multiple lines"
width="200px"/>
You can set label parameters in the screen controller. To do this, you should specify a component identifier and get a reference to the component in the controller:
<label id="dynamicLabel"/>
@Inject
private Label dynamicLabel;
@Subscribe
protected void onInit(InitEvent event) {
dynamicLabel.setValue("Some value");
}
The Label
component can display value of an entity attribute. For this purpose, dataContainer and property attributes are used. For example:
<data>
<instance id="customerDc" class="com.company.sales.entity.Customer" view="_local">
<loader/>
</instance>
</data>
<layout>
<label dataContainer="customerDc" property="name"/>
</layout>
In the example above, component displays the name
attribute of the Customer
entity located in the customerDc
data container.
htmlEnabled
attribute indicates the way the value attribute will be interpreted: if htmlEnabled="true"
, the attribute will be treated as HTML code, otherwise as a plain string.
The htmlSanitizerEnabled
attribute enables or disables HTML sanitization. If htmlEnabled and htmlSanitizerEnabled
attributes are set to true, then the label value will be sanitized.
protected static final String UNSAFE_HTML = "<i>Jackdaws </i><u>love</u> <font size=\"javascript:alert(1)\" " +
"color=\"moccasin\">my</font> " +
"<font size=\"7\">big</font> <sup>sphinx</sup> " +
"<font face=\"Verdana\">of</font> <span style=\"background-color: " +
"red;\">quartz</span><svg/onload=alert(\"XSS\")>";
@Inject
private Label<String> label;
@Subscribe
public void onInit(InitEvent event) {
label.setHtmlEnabled(true);
label.setHtmlSanitizerEnabled(true);
label.setValue(UNSAFE_HTML);
}
The htmlSanitizerEnabled
attribute overrides the value of global cuba.web.htmlSanitizerEnabled configuration property.
- Label styles
-
In Web Client with a Halo-based theme, you can set predefined styles to the
Label
component using thestylename
attribute either in the XML descriptor or in the screen controller:<label value="Label to be styled" stylename="colored"/>
When setting a style programmatically, select one of the
HaloTheme
class constants with theLABEL_
prefix:label.setStyleName(HaloTheme.LABEL_COLORED);
-
bold
- bolder font weight. Suitable for important/prominent UI text.
-
colored
- colored text.
-
failure
- failure badge style. Adds a border around the label and an icon next to the text. Suitable for UI notifications that need to be used in the direct context of some component.
-
h1
- header style for main application headings.
-
h2
- header style for different sections in the application.
-
h3
- header style for different sub-sections in the application.
-
h4
- header style for different sub-sections in the application.
-
light
- lighter font weight. Suitable for additional/supplementary UI text.
-
no-margin
- removes default margins from the header.
-
spinner
- spinner style. Add this style name to an emptyLabel
to create a spinner.
-
success
- success badge style. Adds a border around the label and an icon next to the text. Suitable for UI notifications that need to be used in the direct context of some component.
-
- Attributes of label
-
align - css - dataContainer - description - descriptionAsHtml - enable - box.expandRatio - height - htmlEnabled - htmlSanitizerEnabled - icon - id - property - stylename - value - visible - width
- Elements of label
- Predefined styles of label
-
bold - colored - failure - h1 - h2 - h3 - h4 - huge - large - light - no-margin - small - spinner - success - tiny
- API