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 parent-origin and anchor-point properties.
40 | (ParentOrigin::TOP_LEFT, AnchorPoint::TOP_LEFT) |   |
44 By default TextLabel will automatically select a suitable font from the platform.
45 Typically fonts do not support all scripts, for example Latin fonts often do not provide Arabic glyphs.
46 Therefore you should expect TextLabel to select different fonts for each script.
48 Alternatively a font may be requested using either or all of FONT_FAMILY, FONT_STYLE, and POINT_SIZE properties:
53 label.SetProperty( TextLabel::Property::FONT_FAMILY, "HelveticaNue" );
54 label.SetProperty( TextLabel::Property::FONT_STYLE, "Regular" );
55 label.SetProperty( TextLabel::Property::POINT_SIZE, 12.0f );
61 label.fontFamily = "HelveticaNue";
62 label.fontStyle = "Regular";
66 However the TextLabel will fall-back to using the default font, if the requested font does not support the required scripts.
70 Setting a font size programmatically is not ideal for applications which support multiple screen resolutions etc.
71 A more flexible approach is to prepare various JSON stylesheets, and request a different style for each platform:
75 StyleManager styleManager = StyleManager::Get();
76 styleManager.RequestThemeChange( "example-path/example.json" );
79 To change the font for standard text labels, this JSON syntax can be used:
87 "font-family":"Arial",
88 "font-style":"Regular",
95 However the same point-size is unlikely to be suitable for all labels in an application.
96 To set custom sizes simply set a "style name" for each case, and then provide a style override in JSON:
101 label.SetProperty( Control::Property::STYLE_NAME, "custom" );
106 label.styleName = "custom"';
115 "font-family":"Arial",
116 "font-style":"Regular",
128 In the example above, standard text labels will have point-size 8, and "custom" labels will have point-size 10.
132 Wrapping can be enabled using the MULTI_LINE property:
137 label.SetProperty( TextLabel::Property::MULTI_LINE, true );
143 label.mutliLine = true;
146 The text can be either aligned horizontally to the beginning, end, or center of the available area:
151 label.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, "BEGIN" ); // "CENTER" or "END"
157 label.HorizontalAlignment = "BEGIN"; // "CENTER" or "END"
162 | Here is the "BEGIN" alignment shown for left-to-right (Latin) | right-to-left (Arabic) scripts |
163 |   |   |
164 | Here is the "CENTER" alignment shown for left-to-right (Latin) | right-to-left (Arabic) scripts:|
165 |   |   |
166 | Here is the "END" alignment shown for left-to-right (Latin) | right-to-left (Arabic) scripts:|
167 |   |   |
170 The examples above assume that the TextLabel size greater than the minimum required.
171 The next section provides details about the other size related options.
175 \link size-negotiation Size negotiation \endlink is a layouting feature supported by UI controls such as TextLabel.
177 There are several resize policies which are commonly used with TextLabels.
179 The following examples show TextLabels actual size by setting a colored background, whilst the black area represents the size of the parent control:
181 ### Using natural size
183 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.
184 Therefore in this example the same result would be displayed, regardless of the alignment or multi-line properties.
189 TextLabel label = TextLabel::New( "Hello World" );
190 label.SetAnchorPoint( AnchorPoint::TOP_LEFT );
191 label.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
192 label.SetBackgroundColor( Color::BLUE );
193 Stage::GetCurrent().Add( label );
199 var label = new dali.Textlabel;
200 label.text = "Hello World";
201 label.anchorPoint = dali.TOP_LEFT;
203 label.widthResizePolicy = "USE_NATURAL_SIZE";
204 label.heightResizePolicy = "USE_NATURAL_SIZE";
206 label.textColor = dali.COLOR_WHITE;
207 // background color not available as it's currently not a property of control
208 dali.stage.add( label );
212 
213 
216 ### Height-for-width negotiation
218 To layout text labels vertically, a fixed (maximum) width should be provided by the parent control.
219 Each TextLabel will then report a desired height for the given width.
220 Here is an example of this behavior using TableView as the parent:
224 TableView parent = TableView::New( 3, 1 );
225 parent.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
226 parent.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
227 parent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
228 Stage::GetCurrent().Add( parent );
230 TextLabel label = TextLabel::New( "Hello World" );
231 label.SetAnchorPoint( AnchorPoint::TOP_LEFT );
232 label.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
233 label.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
234 label.SetBackgroundColor( Color::BLUE );
235 parent.AddChild( label, TableView::CellPosition( 0, 0 ) );
236 parent.SetFitHeight( 0 );
238 label = TextLabel::New( "A Quick Brown Fox Jumps Over The Lazy Dog" );
239 label.SetAnchorPoint( AnchorPoint::TOP_LEFT );
240 label.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
241 label.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
242 label.SetBackgroundColor( Color::GREEN );
243 label.SetProperty( TextLabel::Property::MULTI_LINE, true );
244 parent.AddChild( label, TableView::CellPosition( 1, 0 ) );
245 parent.SetFitHeight( 1 );
247 label = TextLabel::New( "لإعادة ترتيب الشاشات، يجب تغيير نوع العرض إلى شبكة قابلة للتخصيص." );
248 label.SetAnchorPoint( AnchorPoint::TOP_LEFT );
249 label.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
250 label.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
251 label.SetBackgroundColor( Color::BLUE );
252 label.SetProperty( TextLabel::Property::MULTI_LINE, true );
253 parent.AddChild( label, TableView::CellPosition( 2, 0 ) );
254 parent.SetFitHeight( 2 );
257 
258 
261 Note that the "Hello World" text label (above) has been given the full width, not the natural width.
263 ### TextLabel Decorations
267 To change the color of the text, the recommended way is to use the TEXT_COLOR property.
268 Note that unlike the Actor::COLOR property, this will not affect child Actors added to the TextLabel.
272 label.SetProperty( TextLabel::Property::TEXT, "Red Text" );
273 label.SetProperty( TextLabel::Property::TEXT_COLOR, Color::RED );
279 label.text = "Red Text";
280 label.textColor = dali.COLOR_RED;
283 
288 To add a drop-shadow to the text, simply set the SHADOW_OFFSET property with non-zero values.
289 The color can also be selected using the SHADOW_COLOR property.
294 stage.SetBackgroundColor( Color::BLUE );
296 label1.SetProperty( TextLabel::Property::TEXT, "Plain Text" );
298 label2.SetProperty( TextLabel::Property::TEXT, "Text with Shadow" );
299 label2.SetProperty( TextLabel::Property::SHADOW_OFFSET, Vector2( 1.0f, 1.0f ) );
300 label2.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
302 label3.SetProperty( TextLabel::Property::TEXT, "Text with Bigger Shadow" );
303 label3.SetProperty( TextLabel::Property::SHADOW_OFFSET, Vector2( 2.0f, 2.0f ) );
304 label3.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
306 label4.SetProperty( TextLabel::Property::TEXT, "Text with Color Shadow" );
307 label4.SetProperty( TextLabel::Property::SHADOW_OFFSET, Vector2( 1.0f, 1.0f ) );
308 label4.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::RED );
314 dali.stage.setBackgroundColor( dali.COLOR_BLUE );
316 label1.text = "Plain Text";
319 label2.text = "Text with Shadow";
320 label2.shadowOffset = [1, 1];
321 label2.shadowColor = dali.COLOR_BLACK;
323 label3.text = "Text with Bigger Shadow";
324 label3.shadowOffset = [2, 2];
325 label3.shadowColor = dali.COLOR_BLACK;
327 label4.SetProperty( TextLabel::Property::TEXT, "Text with Color Shadow" );
328 label3.shadowOffset = [1, 1];
329 label3.shadowColor = dali.COLOR_RED;
333 
337 
338 
340 
341 
344 
345 
350 The text can be underlined by setting UNDERLINE_ENABLED.
351 The color can also be selected using the UNDERLINE_COLOR property.
355 label1.SetProperty( TextLabel::Property::TEXT, "Text with Underline" );
356 label1.SetProperty( TextLabel::Property::UNDERLINE_ENABLED, true );
358 label2.SetProperty( TextLabel::Property::TEXT, "Text with Color Underline" );
359 label2.SetProperty( TextLabel::Property::UNDERLINE_ENABLED, true );
360 label2.SetProperty( TextLabel::Property::UNDERLINE_COLOR, Color::GREEN );
364 label1.Text = "Text with Underline";
365 label1.underlineEnabled = true;
367 label2.Text = "Text with Color Underline";
368 label2.underlineEnabled = true;
369 label2.underlineColor = dali.COLOR_GREEN;
373 
374 
377 
378 
380 By default the underline height will be taken from the font metrics, however this can be overridden using the UNDERLINE_HEIGHT property:
385 label1.SetProperty( TextLabel::Property::UNDERLINE_HEIGHT, 1.0f );
391 label1.underlineHeight = 1;
394 
395 
397 ### Text Label Properties
399 Name (JavaScript) | Name (C++) | Type | Writable | Animatable
400 ---------------------|---------------------|--------------|--------------|-----------
401 renderingBackend | RENDERING_BACKEND | INTEGER | O | X
402 text | TEXT | STRING | O | X
403 fontFamily | FONT_FAMILY | STRING | O | X
404 fontStyle | FONT_STYLE | STRING | O | X
405 pointSize | POINT_SIZE | FLOAT | O | X
406 multiLine | MULTI_LINE | BOOLEAN | O | X
407 horizontalAlignment | HORIZONTAL_ALIGNMENT| STRING | O | X
408 verticalAlignment | VERTICAL_ALIGNMENT | STRING | O | X
409 textColor | TEXT_COLOR | VECTOR4 | O | X
410 shadowOffset | SHADOW_OFFSET | VECTOR2 | O | X
411 shadowColor | SHADOW_COLOR | VECTOR4 | O | X
412 underlineEnabled | UNDERLINE_ENABLED | BOOLEAN | O | X
413 underlineColor | UNDERLINE_COLOR | VECTOR4 | O | X
414 underlineHeight | UNDERLINE_HEIGHT | FLOAT | O | X