If you do not know the width or length of the text, set the label instance’s autoSize property to one of the constants in the TextFieldAutoSize class. By setting the autoSize property, the label is resized automatically to match the contents.
Note: Whenever a Label instance is resized, the resize event (represented by the fl.events.ComponentEvent.RESIZE constant) is dispatched.
The following are valid values for the autoSize property:
TextFieldAutoSize.NONE(default): The label is not resized or aligned to fit the text.TextFieldAutoSize.LEFT: The right and bottom sides of the label are resized to fit the text. The left and top sides are not resized.TextFieldAutoSize.CENTER: The left and right sides of the label resize to fit the text. The horizontal center of the label stays anchored at its original horizontal center position.TextFieldAutoSize.RIGHT: The left and bottom sides of the label are resized to fit the text. The top and right sides are not resized.
The following example resizes a Label instance automatically by setting the autoSize property to TextFieldAutoSize.LEFT.
Full code after the jump.
// ActionScript 3.0 /* Requires: * A Label control in your Flash library. */ import fl.controls.Label; var myLabel:Label = new Label(); myLabel.text = "The quick brown fox jumped over the lazy dog"; myLabel.autoSize = TextFieldAutoSize.LEFT; myLabel.move(10, 10); addChild(myLabel);
The following example resizes a Label instance automatically by setting the autoSize property and listens for the resize event to be dispatched:
// ActionScript 3.0 /* Requires: * A Label control in your Flash library. */ // Import the required component classes. import fl.controls.Label; import fl.events.ComponentEvent; /* Create a new Label component instance, set the text 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.autoSize = TextFieldAutoSize.LEFT; myLabel.move(10, 10); myLabel.addEventListener(ComponentEvent.RESIZE, resizeHandler); addChild(myLabel); /* Create a new Label component instance, set the autoSize property, and add the label to the display list. This label is used to display the width and height of the myLabel instance. */ var debugLabel:Label = new Label(); debugLabel.text = ""; debugLabel.autoSize = TextFieldAutoSize.LEFT; debugLabel.move(10, 30); addChild(debugLabel); /* Handler function for the myLabel instance. This function gets called when the myLabel instance is resized, and sets the text in the debugLabel instance with the width and height of the myLabel instance. */ function resizeHandler(evt:ComponentEvent):void { var lbl:Label = evt.currentTarget as Label; debugLabel.text = "[" + evt.type + "] width:" + myLabel.width + ", height:" + myLabel.height; }
For more information on the Flash/ActionScript 3.0 Label component, see the “Using the Label component” Flash Quick Start on Adobe.com.
{ 2 comments… read them below or add one }
you have mistake all over the code
Anonymous,
Thanks. I found one typo and updated it in the code above (
eventshould have beenevtin the last example). Both examples now work for me when I tested them in Flash CS4/ActionScript 3.0 as long as I have a Label control in my Flash library.Let me know if you are still seeing errors.
Peter