Creating a selectable Label instance in Flash using ActionScript 3.0
By default, the text in a Label component instance isn’t selectable by the user. If you want to allow a user to select the text in a label, set the label’s selectable property to true.
The following example creates a selectable Label instance by setting the selectable property to true.
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 selectable property, 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.selectable = true; myLabel.move(10, 10); addChild(myLabel);
The following example creates two Label component instances. Both instances have their selectable and alwaysShowSelection properties set to true, which allows the instance to display the currently selected text even if the label doesn’t currently have focus:
// ActionScript 3.0 // Import the required component classes. import fl.controls.Label; /* Create a new Label component instance, set the selectable and textField.alwaysShowSelection properties, call the setSelection() method on the textField property, 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.selectable = true; myLabel.textField.alwaysShowSelection = true; myLabel.textField.setSelection(16, 19); myLabel.move(10, 10); addChild(myLabel); /* Create a new Label component instance, set the selectable and textField.alwaysShowSelection properties, call the setSelection() method on the textField property, and add the label to the display list. */ var secondLabel:Label = new Label(); secondLabel.text = "The quick brown fox jumped over the lazy dog"; secondLabel.wordWrap = true; secondLabel.autoSize = TextFieldAutoSize.LEFT; secondLabel.width = 150; secondLabel.selectable = true; secondLabel.textField.alwaysShowSelection = true; secondLabel.textField.setSelection(41, 44); secondLabel.move(170, 10); addChild(secondLabel);
Note: The alwaysShowSelection property can’t be set directly on the Label instance. In order to set this property you must set it on the Label instance’s textField property, which contains a reference to the Label instance’s internal text field.
For more information on the Flash/ActionScript 3.0 Label component, see the “Using the Label component” Flash Quick Start on Adobe.com.
