If you need to display longer amounts of text but don’t want to use a TextArea or TextField, set the label instance’s wordWrap property to true to enable multi-line text labels.
The following example creates a new multi-line label instance by setting the wordWrap property to true. This example sets the height of the Label instance manually. The previous examples automatically resize the label instance by setting the autoSize property.
Full code after the jump.
// ActionScript 3.0 // Import the required component classes. import fl.controls.Label; /* Create a new Label component instance, set the wordWrap and height properties, and add the label to the display list. */ var myLabel:Label = new Label(); myLabel.text = "The quick brown fox jumped over the lazy dog"; myLabel.wordWrap = true; myLabel.height = 60; myLabel.move(10, 10); addChild(myLabel);
Note: Even with the wordWrap property set to true you must either set the height property or set the label instance’s autoSize property to resize the label vertically; otherwise, the text may be cropped off.
The following example creates a multi-line label instance and sets the autoSize property, which causes the instance to automatically resize vertically:
// ActionScript 3.0 // Import the required component classes. import fl.controls.Label; /* Create a new Label component instance, set the wordWrap and autoSize properties, and add the label to the display list. */ var myLabel:Label = new Label(); myLabel.text = "The quick brown fox jumped over the lazy dog"; myLabel.wordWrap = true; myLabel.autoSize = TextFieldAutoSize.LEFT; myLabel.move(10, 10); addChild(myLabel);
The following example creates a multi-line Label instance, sets the width property to 150 and sets the autoSize property, which causes the instance to automatically resize vertically:
// ActionScript 3.0 // Import the required component classes. import fl.controls.Label; /* Create a new Label component instance, set the wordWrap, autoSize, and width properties, and add the label to the display list. */ var myLabel:Label = new Label(); myLabel.text = "The quick brown fox jumped over the lazy dog"; myLabel.wordWrap = true; myLabel.autoSize = TextFieldAutoSize.LEFT; myLabel.width = 150; myLabel.move(10, 10); addChild(myLabel);
Note: By setting the width property in the previous example, the label wraps at 150 pixels instead of the default label width of 100 pixels.
For more information on the Flash/ActionScript 3.0 Label component, see the “Using the Label component” Flash Quick Start on Adobe.com.
{ 5 comments… read them below or add one }
The Label doesn’t have a wordWrap or autoSize property… what am I missing??
RJ, I agree, this example appears to be wrong.
Unless I am missing something.
Look at the class, it loos like it’s a custom class or from another package,
import fl.controls.Label;
For those interested, I had to do the following to format the label for multiline:
var myLabel:Label = new Label();
myLabel.textField.wordWrap = true;
myLabel.textField.autoSize = TextFieldAutoSize.LEFT;
etc…
Hi,
I have a data grid for which i have a a custom item renderer which extends a label.
i tried to make it multiline automatically but that doesnt seem to work.
Posting snippet of my renderer here:-
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
super.updateDisplayList(unscaledWidth, unscaledHeight);
super.height=45;
this.textField.wordWrap=true;
if(data.hasOwnProperty(“publishedStatus”)){
//setStyle(“color”, (data.isEdit == true ) ? EDIT_COLOR : NONEDIT_COLOR);
var g:Graphics = graphics;
g.clear();
if ((data.publishedStatus == 1)||(data.publishedStatus == 2))
{
g.beginFill(PREVIEW_BACKGROUND_COLOR);
g.drawRect(0, 0, unscaledWidth, unscaledHeight);
g.endFill();
} else if (data.publishedStatus == 0){
g.beginFill(SCRAP_BACKGROUND_COLOR);
g.drawRect(0, 0, unscaledWidth, unscaledHeight);
g.endFill();
}
}
}
Instead of
super.height=45;
this.textField.wordWrap=true;
i tried using below:-
this.textField.wordWrap=true;
this.textField.autoSize= “left”;
That doesnt help as the height is not changed aotumatically.
Any clue why???
Thanks in Advance,
Anil