# Titanium.UI.Label
A text label, with an optional background image.
# Overview
Use the Titanium.UI.createLabel method or <Label>
Alloy element to create a label.
# Examples
# Basic Label
Create a center-aligned label with a text shadow and specified-size font, and another containing forced line breaks and Unicode symbols.
var win = Ti.UI.createWindow({
backgroundColor: 'white',
exitOnClose: true,
fullscreen: false,
layout: 'vertical',
title: 'Label Demo'
});
var label1 = Ti.UI.createLabel({
color: '#900',
font: { fontSize:48 },
shadowColor: '#aaa',
shadowOffset: {x:5, y:5},
shadowRadius: 3,
text: 'A simple label',
textAlign: Ti.UI.TEXT_ALIGNMENT_CENTER,
top: 30,
width: Ti.UI.SIZE, height: Ti.UI.SIZE
});
var label2 = Ti.UI.createLabel({
color:'blue',
text: 'A long label with\na few line breaks\nand Unicode (UTF8)\nsymbols such as\na white chess piece \u2655\nand the euro symbol \u20ac\nlooks like this!\n',
textAlign: Ti.UI.TEXT_ALIGNMENT_LEFT,
top: 30,
width: 300, height: 200
});
win.add(label1);
win.add(label2);
win.open();
# Alloy XML Markup
Previous example as an Alloy view.
The font and shadowOffset properties need to be defined in the TSS or controller file. These property values are dictionaries, rather than single values, so they cannot be included in the markup.
<Alloy>
<Window id="win" backgroundColor="white" exitOnClose="true" fullscreen="false" layout="vertical" title="Label Demo">
<!-- The text property can either be defined as an attribute or as node text. -->
<Label id="label1" color="#900" shadowColor="#aaa" text="A simple label" textAlign="Ti.UI.TEXT_ALIGNMENT_CENTER"
top="30" width="Ti.UI.SIZE" height="Ti.UI.SIZE" />
<Label id="label2" color="blue" textAlign="Ti.UI.TEXT_ALIGNMENT_LEFT" top="30" width="300" height="200">
A long label with\na few line breaks\nand Unicode (UTF8)\nsymbols such as\na white chess piece \u2655\nand the euro symbol \u20ac\nlooks like this!\n
</Label>
</Window>
</Alloy>
# Properties
# attributedString
Specify an attributed string for the label.
The underlying attributed string drawn by the label. If set, avoid setting common attributes
in the label, such as color
and font
, as unexpected behaviors may result.
Prior to Release 3.6.0, assign this property an object from the <Titanium.UI.iOS.AttributedString> class.
Since Appcelerator CLI 4.1.0 (Alloy 1.7.0), for Alloy, you can use an <AttributedString>
element inside a <Label>
element and set the text property as node text:
<Alloy>
<Window>
<Label>
<AttributedString>
Alloy is great!
</AttributedString>
</Label>
</Window>
</Alloy>
Then set attributes in the TSS file:
"AttributedString" : {
attributes: [
{
type: Ti.UI.ATTRIBUTE_FOREGROUND_COLOR,
value: 'red',
range: [0, 5]
},
{
type: Ti.UI.ATTRIBUTE_UNDERLINES_STYLE,
value: Ti.UI.ATTRIBUTE_UNDERLINE_STYLE_SINGLE,
range: [9, 5]
}
]
}
# autoLink
Automatically convert certain text items in the label to clickable links.
Multiple autolink values can be combined with a bitwise OR. For example:
label.autoLink = Ti.UI.AUTOLINK_MAP_ADDRESSES|Ti.UI.AUTOLINK_PHONE_NUMBERS;
Prior to Release 3.0, this field used platform-specific constants. These are now
deprecated in favor of the AUTOLINK
constants.
The Android-specific LINKIFY
legacy constants are defined in Titanium.UI.Android.
# backgroundPaddingBottom
Number of pixels to extend the background image past the label on the bottom.
# backgroundPaddingLeft
Number of pixels to extend the background image past the label on the left.
# backgroundPaddingRight
Number of pixels to extend the background image past the label on the right.
# backgroundPaddingTop
Number of pixels to extend the background image past the label on the top.
# color
Color of the label text, as a color name or hex triplet.
For information about color values, see the "Colors" section of Titanium.UI.
# ellipsize
Causes words in the text that are longer than the view is wide to be ellipsized instead of broken in the middle.
- Titanium.UI.TEXT_ELLIPSIZE_TRUNCATE_WORD_WRAP
- Titanium.UI.TEXT_ELLIPSIZE_TRUNCATE_CHAR_WRAP
- Titanium.UI.TEXT_ELLIPSIZE_TRUNCATE_CLIP
- Titanium.UI.TEXT_ELLIPSIZE_TRUNCATE_START
- Titanium.UI.TEXT_ELLIPSIZE_TRUNCATE_MIDDLE
- Titanium.UI.TEXT_ELLIPSIZE_TRUNCATE_END
- Titanium.UI.TEXT_ELLIPSIZE_TRUNCATE_MARQUEE
- Titanium.UI.TEXT_ELLIPSIZE_TRUNCATE_NONE
Default: Titanium.UI.TEXT_ELLIPSIZE_TRUNCATE_END
# highlightedColor
Color of the label when in the highlighted state, as a color name or hex triplet.
For information about color values, see the "Colors" section of Titanium.UI.
Used when the label is part of a view that can be highlighted, such as a Titanium.UI.TableViewRow.
# includeFontPadding
Includes extra top and bottom padding to make room for accents that go above normal ascent and descent.
Default: undefined but behaves as true
# lineSpacing
Line spacing of the text, as a dictionary with the properties add
and multiply
.
# minimumFontSize
Minimum font size when the font is sized based on the contents.
When set to a valid font size, this property enables single-line mode and the font will be auto-downscaled so that its text will fit the label's width if necessary. Will not downscale the font lower than the assigned minimum font size.
When set to less than or equal to zero, the font auto-downscaling feature will be disabled.
# shadowColor
Shadow color of the text, as a color name or hex triplet.
Use in conjunction with shadowOffset and shadowRadius. For information about color values, see the "Colors" section of Titanium.UI.
# shadowOffset
Shadow offset of the text, as a dictionary with the properties x
and y
.
Use in conjunction with shadowColor and shadowRadius.
# shadowRadius
Shadow radius of the text.
Use in conjunction with shadowColor and shadowOffset.
# text
Label text.
Use the backslash and letter n
line feed character combination, ie \n
, to force a new
line.
Use Unicode characters, such as those included in (but not limited to) the Unicode List of Useful Symbols section of wikipedia, to insert special characters and symbols.
See the example in Titanium.UI.Label for a demonstration.
# textAlign
Text alignment.
# textid
Key identifying a string from the locale file to use for the label text.
Only one of text
or textid
should be specified.
# verticalAlign
Vertical text alignment, specified using one of the vertical alignment constants from Titanium.UI.
On iPhone and iPad the default value of the property is undefined. The behavior of the label depends on the actual content height. If the actual content height is larger than the available height the text is truncated at the end and the content is center aligned.
On Android, the default value of the property is undefined, and the default behavior when the property is undefined is to center the text vertically.
Default: undefined
# Events
# link
Fired when user interacts with a URL in the Label.
The URL is set using the ATTRIBUTE_LINK property on the attributedString.
On Android, this event also fires if the html
property is used instead of the attributedString
property. This event will
also override the default behavior of openning the link in its default application.
On iOS, this is only valid on version 7 and above. In previous versions of the the Titanium SDK this event required a longpress
gesture to be performed. Beginning with Titanium SDK 4.0.0, only a singletap
gesture is required to invoke this event.
Properties
Name | Type | Description |
---|---|---|
url | String | The URL that is associated with this event. |
range | Array | An array of two numbers [location, length] describing the character range of the text associated with this URL. |
bubbles | Boolean | This is false. This event does not bubble |
source | Object | Source object that fired the event. |
type | String | Name of the event fired. |
bubbles | Boolean | True if the event will try to bubble up if possible. |
cancelBubble | Boolean | Set to true to stop the event from bubbling. |