4 # Text Label {#text-label}
8 The Dali::Toolkit::TextLabel is a Dali::Toolkit::Control which renders a short text string.
9 Text labels are lightweight, non-editable and do not respond to user input.
13 To display a TextLabel the TEXT property must be set using a UTF-8 string.
18 TextLabel label = TextLabel::New();
19 label.SetProperty( TextLabel::Property::TEXT, "Hello World" );
20 label.SetAnchorPoint( AnchorPoint::TOP_LEFT );
21 Stage::GetCurrent().Add( label );
27 var label = new dali.TextLabel();
29 label.text = "Hello World";
30 label.anchorPoint = dali.TOP_LEFT;
32 dali.stage.add( label );
35 The label must also be added to the stage, or to an actor which is on the stage.
36 The position of the label on-screen is dependent on the parentOrigin and anchorPoint properties.
40 | (ParentOrigin::TOP_LEFT, AnchorPoint::TOP_LEFT) | ![ ](../assets/img/text-controls/TextLabelTopLeft.png) ![ ](TextLabelTopLeft.png) |
44 By default TextLabel will automatically select a suitable font from the platform. However, a different font could be selected. See the [Font Selection](@ref font-selection) section for more details.
48 Mark-up tags can be used to change the style of the text. See the [Mark-up Style](@ref markup-style) section for more details.
52 Wrapping can be enabled using the MULTI_LINE property:
57 label.SetProperty( TextLabel::Property::MULTI_LINE, true );
63 label.mutliLine = true;
66 The text can be either aligned horizontally to the beginning, end, or center of the available area:
71 label.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, "BEGIN" ); // "CENTER" or "END"
77 label.HorizontalAlignment = "BEGIN"; // "CENTER" or "END"
82 | Here is the "BEGIN" alignment shown for left-to-right (Latin) | right-to-left (Arabic) scripts |
83 | ![ ](../assets/img/text-controls/LatinBegin.png) ![ ](LatinBegin.png) | ![ ](../assets/img/text-controls/ArabicBegin.png) ![ ](ArabicBegin.png) |
84 | Here is the "CENTER" alignment shown for left-to-right (Latin) | right-to-left (Arabic) scripts:|
85 | ![ ](../assets/img/text-controls/LatinCenter.png) ![ ](LatinCenter.png) | ![ ](../assets/img/text-controls/ArabicCenter.png) ![ ](ArabicCenter.png) |
86 | Here is the "END" alignment shown for left-to-right (Latin) | right-to-left (Arabic) scripts:|
87 | ![ ](../assets/img/text-controls/LatinEnd.png) ![ ](LatinEnd.png) | ![ ](../assets/img/text-controls/ArabicEnd.png) ![ ](ArabicEnd.png) |
90 The examples above assume that the TextLabel size greater than the minimum required.
91 The next section provides details about the other size related options.
95 \link size-negotiation Size negotiation \endlink is a layouting feature supported by UI controls such as TextLabel.
97 There are several resize policies which are commonly used with TextLabels.
99 The following examples show TextLabels actual size by setting a colored background, whilst the black area represents the size of the parent control:
101 ### Using natural size
103 With a "natural" size TextLabel will be large enough to display the text without wrapping, and will not have extra space to align the text within.
104 Therefore in this example the same result would be displayed, regardless of the alignment or multi-line properties.
109 TextLabel label = TextLabel::New( "Hello World" );
110 label.SetAnchorPoint( AnchorPoint::TOP_LEFT );
111 label.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
112 label.SetBackgroundColor( Color::BLUE );
113 Stage::GetCurrent().Add( label );
119 var label = new dali.Textlabel;
120 label.text = "Hello World";
121 label.anchorPoint = dali.TOP_LEFT;
123 label.widthResizePolicy = "USE_NATURAL_SIZE";
124 label.heightResizePolicy = "USE_NATURAL_SIZE";
126 label.textColor = dali.COLOR_WHITE;
127 // background color not available as it's currently not a property of control
128 dali.stage.add( label );
132 ![ ](../assets/img/text-controls/HelloWorld-NaturalSize.png)
133 ![ ](HelloWorld-NaturalSize.png)
136 ### Height-for-width negotiation
138 To layout text labels vertically, a fixed (maximum) width should be provided by the parent control.
139 Each TextLabel will then report a desired height for the given width.
140 Here is an example of this behavior using TableView as the parent:
144 TableView parent = TableView::New( 3, 1 );
145 parent.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
146 parent.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
147 parent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
148 Stage::GetCurrent().Add( parent );
150 TextLabel label = TextLabel::New( "Hello World" );
151 label.SetAnchorPoint( AnchorPoint::TOP_LEFT );
152 label.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
153 label.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
154 label.SetBackgroundColor( Color::BLUE );
155 parent.AddChild( label, TableView::CellPosition( 0, 0 ) );
156 parent.SetFitHeight( 0 );
158 label = TextLabel::New( "A Quick Brown Fox Jumps Over The Lazy Dog" );
159 label.SetAnchorPoint( AnchorPoint::TOP_LEFT );
160 label.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
161 label.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
162 label.SetBackgroundColor( Color::GREEN );
163 label.SetProperty( TextLabel::Property::MULTI_LINE, true );
164 parent.AddChild( label, TableView::CellPosition( 1, 0 ) );
165 parent.SetFitHeight( 1 );
167 label = TextLabel::New( "لإعادة ترتيب الشاشات، يجب تغيير نوع العرض إلى شبكة قابلة للتخصيص." );
168 label.SetAnchorPoint( AnchorPoint::TOP_LEFT );
169 label.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
170 label.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
171 label.SetBackgroundColor( Color::BLUE );
172 label.SetProperty( TextLabel::Property::MULTI_LINE, true );
173 parent.AddChild( label, TableView::CellPosition( 2, 0 ) );
174 parent.SetFitHeight( 2 );
177 ![ ](../assets/img/text-controls/HelloWorld-HeightForWidth.png)
178 ![ ](HelloWorld-HeightForWidth.png)
181 Note that the "Hello World" text label (above) has been given the full width, not the natural width.
183 ### TextLabel Decorations
187 To change the color of the text, the recommended way is to use the TEXT_COLOR property.
188 Note that unlike the Actor::COLOR property, this will not affect child Actors added to the TextLabel.
192 label.SetProperty( TextLabel::Property::TEXT, "Red Text" );
193 label.SetProperty( TextLabel::Property::TEXT_COLOR, Color::RED );
199 label.text = "Red Text";
200 label.textColor = dali.COLOR_RED;
203 ![ ](../assets/img/text-controls/RedText.png)
208 To add a drop-shadow to the text, simply set the SHADOW_OFFSET property with non-zero values.
209 The color can also be selected using the SHADOW_COLOR property.
214 stage.SetBackgroundColor( Color::BLUE );
216 label1.SetProperty( TextLabel::Property::TEXT, "Plain Text" );
218 label2.SetProperty( TextLabel::Property::TEXT, "Text with Shadow" );
219 label2.SetProperty( TextLabel::Property::SHADOW_OFFSET, Vector2( 1.0f, 1.0f ) );
220 label2.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
222 label3.SetProperty( TextLabel::Property::TEXT, "Text with Bigger Shadow" );
223 label3.SetProperty( TextLabel::Property::SHADOW_OFFSET, Vector2( 2.0f, 2.0f ) );
224 label3.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
226 label4.SetProperty( TextLabel::Property::TEXT, "Text with Color Shadow" );
227 label4.SetProperty( TextLabel::Property::SHADOW_OFFSET, Vector2( 1.0f, 1.0f ) );
228 label4.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::RED );
234 dali.stage.setBackgroundColor( dali.COLOR_BLUE );
236 label1.text = "Plain Text";
239 label2.text = "Text with Shadow";
240 label2.shadowOffset = [1, 1];
241 label2.shadowColor = dali.COLOR_BLACK;
243 label3.text = "Text with Bigger Shadow";
244 label3.shadowOffset = [2, 2];
245 label3.shadowColor = dali.COLOR_BLACK;
247 label4.SetProperty( TextLabel::Property::TEXT, "Text with Color Shadow" );
248 label3.shadowOffset = [1, 1];
249 label3.shadowColor = dali.COLOR_RED;
253 ![ ](../assets/img/text-controls/PlainText.png)
257 ![ ](../assets/img/text-controls/TextWithShadow.png)
258 ![ ](TextWithShadow.png)
260 ![ ](../assets/img/text-controls/TextWithBiggerShadow.png)
261 ![ ](TextWithBiggerShadow.png)
264 ![ ](../assets/img/text-controls/TextWithColorShadow.png)
265 ![ ](TextWithColorShadow.png)
270 The text can be underlined by setting UNDERLINE_ENABLED.
271 The color can also be selected using the UNDERLINE_COLOR property.
275 label1.SetProperty( TextLabel::Property::TEXT, "Text with Underline" );
276 label1.SetProperty( TextLabel::Property::UNDERLINE_ENABLED, true );
278 label2.SetProperty( TextLabel::Property::TEXT, "Text with Color Underline" );
279 label2.SetProperty( TextLabel::Property::UNDERLINE_ENABLED, true );
280 label2.SetProperty( TextLabel::Property::UNDERLINE_COLOR, Color::GREEN );
284 label1.Text = "Text with Underline";
285 label1.underlineEnabled = true;
287 label2.Text = "Text with Color Underline";
288 label2.underlineEnabled = true;
289 label2.underlineColor = dali.COLOR_GREEN;
293 ![ ](../assets/img/text-controls/TextWithUnderline.png)
294 ![ ](TextWithUnderline.png)
297 ![ ](../assets/img/text-controls/TextWithColorUnderline.png)
298 ![ ](TextWithColorUnderline.png)
300 By default the underline height will be taken from the font metrics, however this can be overridden using the UNDERLINE_HEIGHT property:
305 label1.SetProperty( TextLabel::Property::UNDERLINE_HEIGHT, 1.0f );
311 label1.underlineHeight = 1;
314 ![ ](../assets/img/text-controls/TextWith1pxUnderline.png)
315 ![ ](TextWith1pxUnderline.png)
317 ### Text Label Properties
319 Name (JavaScript) | Name (C++) | Type | Writable | Animatable
320 ---------------------|----------------------|--------------|--------------|-----------
321 renderingBackend | RENDERING_BACKEND | INTEGER | O | X
322 text | TEXT | STRING | O | X
323 fontFamily | FONT_FAMILY | STRING | O | X
324 fontStyle | FONT_STYLE | STRING | O | X
325 pointSize | POINT_SIZE | FLOAT | O | X
326 multiLine | MULTI_LINE | BOOLEAN | O | X
327 horizontalAlignment | HORIZONTAL_ALIGNMENT | STRING | O | X
328 verticalAlignment | VERTICAL_ALIGNMENT | STRING | O | X
329 textColor | TEXT_COLOR | VECTOR4 | O | X
330 shadowOffset | SHADOW_OFFSET | VECTOR2 | O | X
331 shadowColor | SHADOW_COLOR | VECTOR4 | O | X
332 underlineEnabled | UNDERLINE_ENABLED | BOOLEAN | O | X
333 underlineColor | UNDERLINE_COLOR | VECTOR4 | O | X
334 underlineHeight | UNDERLINE_HEIGHT | FLOAT | O | X
335 enableMarkup | ENABLE_MARKUP | BOOLEAN | O | X