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 Responses to Automatically resizing a Label instance in Flash using ActionScript 3.0
Leave a Reply Cancel reply
Recent Posts
- Getting started with the TLFTextField class in ActionScript 3.0 and Flash CS5
- Adding tick marks to a Slider control in Flash using ActionScript 3.0
- Creating a vertical Slider control in Flash using ActionScript 3.0
- Creating a custom context menu item in Flash using ActionScript 3.0
- Rotating a Sprite object around its x-axis in Flash using ActionScript 3.0 and Flash Player 10
- Detecting when the user changes the color in a ColorPicker control in Flash using ActionScript 3.0
- Getting the currently selected color as a hexadecimal value on a ColorPicker control in Flash using ActionScript 3.0
- Toggling the text field on the ColorPicker control in Flash using ActionScript 3.0
- Creating a vertical Slider control in Flash using ActionScript 3.0
- Setting the number of columns on a ColorPicker control in Flash using ActionScript 3.0
Categories
- Bitmap (1)
- Components (72)
- Button (19)
- CheckBox (2)
- ColorPicker (6)
- ComboBox (1)
- DataGrid (8)
- FLVPlayback (7)
- Label (9)
- ProgressBar (2)
- Slider (3)
- TextArea (1)
- TextInput (7)
- UILoader (7)
- ContextMenu (1)
- Embed (4)
- ExternalInterface (2)
- Flex (7)
- Font (2)
- General (5)
- Graphics (2)
- JSFL (14)
- Loader (3)
- LoadVars (3)
- Microphone (1)
- migration (12)
- MovieClip (1)
- MovieClipLoader (1)
- Sound (2)
- TextField (1)
- TLFTextField (1)
- TransitionManager (1)
- Tween (1)
- Uncategorized (1)
- URLLoader (4)
- URLVariables (1)
- Video (1)
- XML (2)
Advertising


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