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 Wrapping can be enabled using the MULTI_LINE property:
53 label.SetProperty( TextLabel::Property::MULTI_LINE, true );
59 label.mutliLine = true;
62 The text can be either aligned horizontally to the beginning, end, or center of the available area:
67 label.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, "BEGIN" ); // "CENTER" or "END"
73 label.HorizontalAlignment = "BEGIN"; // "CENTER" or "END"
78 | Here is the "BEGIN" alignment shown for left-to-right (Latin) | right-to-left (Arabic) scripts |
79 | ![ ](../assets/img/text-controls/LatinBegin.png) ![ ](LatinBegin.png) | ![ ](../assets/img/text-controls/ArabicBegin.png) ![ ](ArabicBegin.png) |
80 | Here is the "CENTER" alignment shown for left-to-right (Latin) | right-to-left (Arabic) scripts:|
81 | ![ ](../assets/img/text-controls/LatinCenter.png) ![ ](LatinCenter.png) | ![ ](../assets/img/text-controls/ArabicCenter.png) ![ ](ArabicCenter.png) |
82 | Here is the "END" alignment shown for left-to-right (Latin) | right-to-left (Arabic) scripts:|
83 | ![ ](../assets/img/text-controls/LatinEnd.png) ![ ](LatinEnd.png) | ![ ](../assets/img/text-controls/ArabicEnd.png) ![ ](ArabicEnd.png) |
86 The examples above assume that the TextLabel size greater than the minimum required.
87 The next section provides details about the other size related options.
91 \link size-negotiation Size negotiation \endlink is a layouting feature supported by UI controls such as TextLabel.
93 There are several resize policies which are commonly used with TextLabels.
95 The following examples show TextLabels actual size by setting a colored background, whilst the black area represents the size of the parent control:
97 ### Using natural size
99 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.
100 Therefore in this example the same result would be displayed, regardless of the alignment or multi-line properties.
105 TextLabel label = TextLabel::New( "Hello World" );
106 label.SetAnchorPoint( AnchorPoint::TOP_LEFT );
107 label.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
108 label.SetBackgroundColor( Color::BLUE );
109 Stage::GetCurrent().Add( label );
115 var label = new dali.Textlabel;
116 label.text = "Hello World";
117 label.anchorPoint = dali.TOP_LEFT;
119 label.widthResizePolicy = "USE_NATURAL_SIZE";
120 label.heightResizePolicy = "USE_NATURAL_SIZE";
122 label.textColor = dali.COLOR_WHITE;
123 // background color not available as it's currently not a property of control
124 dali.stage.add( label );
128 ![ ](../assets/img/text-controls/HelloWorld-NaturalSize.png)
129 ![ ](HelloWorld-NaturalSize.png)
132 ### Height-for-width negotiation
134 To layout text labels vertically, a fixed (maximum) width should be provided by the parent control.
135 Each TextLabel will then report a desired height for the given width.
136 Here is an example of this behavior using TableView as the parent:
140 TableView parent = TableView::New( 3, 1 );
141 parent.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
142 parent.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
143 parent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
144 Stage::GetCurrent().Add( parent );
146 TextLabel label = TextLabel::New( "Hello World" );
147 label.SetAnchorPoint( AnchorPoint::TOP_LEFT );
148 label.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
149 label.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
150 label.SetBackgroundColor( Color::BLUE );
151 parent.AddChild( label, TableView::CellPosition( 0, 0 ) );
152 parent.SetFitHeight( 0 );
154 label = TextLabel::New( "A Quick Brown Fox Jumps Over The Lazy Dog" );
155 label.SetAnchorPoint( AnchorPoint::TOP_LEFT );
156 label.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
157 label.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
158 label.SetBackgroundColor( Color::GREEN );
159 label.SetProperty( TextLabel::Property::MULTI_LINE, true );
160 parent.AddChild( label, TableView::CellPosition( 1, 0 ) );
161 parent.SetFitHeight( 1 );
163 label = TextLabel::New( "لإعادة ترتيب الشاشات، يجب تغيير نوع العرض إلى شبكة قابلة للتخصيص." );
164 label.SetAnchorPoint( AnchorPoint::TOP_LEFT );
165 label.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
166 label.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
167 label.SetBackgroundColor( Color::BLUE );
168 label.SetProperty( TextLabel::Property::MULTI_LINE, true );
169 parent.AddChild( label, TableView::CellPosition( 2, 0 ) );
170 parent.SetFitHeight( 2 );
173 ![ ](../assets/img/text-controls/HelloWorld-HeightForWidth.png)
174 ![ ](HelloWorld-HeightForWidth.png)
177 Note that the "Hello World" text label (above) has been given the full width, not the natural width.
179 ### TextLabel Decorations
183 To change the color of the text, the recommended way is to use the TEXT_COLOR property.
184 Note that unlike the Actor::COLOR property, this will not affect child Actors added to the TextLabel.
188 label.SetProperty( TextLabel::Property::TEXT, "Red Text" );
189 label.SetProperty( TextLabel::Property::TEXT_COLOR, Color::RED );
195 label.text = "Red Text";
196 label.textColor = dali.COLOR_RED;
199 ![ ](../assets/img/text-controls/RedText.png)
204 To add a drop-shadow to the text, simply set the SHADOW_OFFSET property with non-zero values.
205 The color can also be selected using the SHADOW_COLOR property.
210 stage.SetBackgroundColor( Color::BLUE );
212 label1.SetProperty( TextLabel::Property::TEXT, "Plain Text" );
214 label2.SetProperty( TextLabel::Property::TEXT, "Text with Shadow" );
215 label2.SetProperty( TextLabel::Property::SHADOW_OFFSET, Vector2( 1.0f, 1.0f ) );
216 label2.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
218 label3.SetProperty( TextLabel::Property::TEXT, "Text with Bigger Shadow" );
219 label3.SetProperty( TextLabel::Property::SHADOW_OFFSET, Vector2( 2.0f, 2.0f ) );
220 label3.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
222 label4.SetProperty( TextLabel::Property::TEXT, "Text with Color Shadow" );
223 label4.SetProperty( TextLabel::Property::SHADOW_OFFSET, Vector2( 1.0f, 1.0f ) );
224 label4.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::RED );
230 dali.stage.setBackgroundColor( dali.COLOR_BLUE );
232 label1.text = "Plain Text";
235 label2.text = "Text with Shadow";
236 label2.shadowOffset = [1, 1];
237 label2.shadowColor = dali.COLOR_BLACK;
239 label3.text = "Text with Bigger Shadow";
240 label3.shadowOffset = [2, 2];
241 label3.shadowColor = dali.COLOR_BLACK;
243 label4.SetProperty( TextLabel::Property::TEXT, "Text with Color Shadow" );
244 label3.shadowOffset = [1, 1];
245 label3.shadowColor = dali.COLOR_RED;
249 ![ ](../assets/img/text-controls/PlainText.png)
253 ![ ](../assets/img/text-controls/TextWithShadow.png)
254 ![ ](TextWithShadow.png)
256 ![ ](../assets/img/text-controls/TextWithBiggerShadow.png)
257 ![ ](TextWithBiggerShadow.png)
260 ![ ](../assets/img/text-controls/TextWithColorShadow.png)
261 ![ ](TextWithColorShadow.png)
266 The text can be underlined by setting UNDERLINE_ENABLED.
267 The color can also be selected using the UNDERLINE_COLOR property.
271 label1.SetProperty( TextLabel::Property::TEXT, "Text with Underline" );
272 label1.SetProperty( TextLabel::Property::UNDERLINE_ENABLED, true );
274 label2.SetProperty( TextLabel::Property::TEXT, "Text with Color Underline" );
275 label2.SetProperty( TextLabel::Property::UNDERLINE_ENABLED, true );
276 label2.SetProperty( TextLabel::Property::UNDERLINE_COLOR, Color::GREEN );
280 label1.Text = "Text with Underline";
281 label1.underlineEnabled = true;
283 label2.Text = "Text with Color Underline";
284 label2.underlineEnabled = true;
285 label2.underlineColor = dali.COLOR_GREEN;
289 ![ ](../assets/img/text-controls/TextWithUnderline.png)
290 ![ ](TextWithUnderline.png)
293 ![ ](../assets/img/text-controls/TextWithColorUnderline.png)
294 ![ ](TextWithColorUnderline.png)
296 By default the underline height will be taken from the font metrics, however this can be overridden using the UNDERLINE_HEIGHT property:
301 label1.SetProperty( TextLabel::Property::UNDERLINE_HEIGHT, 1.0f );
307 label1.underlineHeight = 1;
310 ![ ](../assets/img/text-controls/TextWith1pxUnderline.png)
311 ![ ](TextWith1pxUnderline.png)
313 ### Text Label Properties
315 Name (JavaScript) | Name (C++) | Type | Writable | Animatable
316 ---------------------|---------------------|--------------|--------------|-----------
317 renderingBackend | RENDERING_BACKEND | INTEGER | O | X
318 text | TEXT | STRING | O | X
319 fontFamily | FONT_FAMILY | STRING | O | X
320 fontStyle | FONT_STYLE | STRING | O | X
321 pointSize | POINT_SIZE | FLOAT | O | X
322 multiLine | MULTI_LINE | BOOLEAN | O | X
323 horizontalAlignment | HORIZONTAL_ALIGNMENT| STRING | O | X
324 verticalAlignment | VERTICAL_ALIGNMENT | STRING | O | X
325 textColor | TEXT_COLOR | VECTOR4 | O | X
326 shadowOffset | SHADOW_OFFSET | VECTOR2 | O | X
327 shadowColor | SHADOW_COLOR | VECTOR4 | O | X
328 underlineEnabled | UNDERLINE_ENABLED | BOOLEAN | O | X
329 underlineColor | UNDERLINE_COLOR | VECTOR4 | O | X
330 underlineHeight | UNDERLINE_HEIGHT | FLOAT | O | X