Creating and removing text fields dynamically

You can use the createTextField method of the MovieClip object to create a new, empty text field as a child of the movie clip that calls the method. You can use the removeTextField method to remove a text field created with createTextField; it will not work on a text field placed by the Timeline.

When you create a text field, you can use the TextField object to set properties of the text field. If you don't set the properties, the new text field receives a set of default properties. The default properties of the new text field are as follows:

type = "dynamic"
border = false
background = false
password = false
multiline = false
html = false
embedFonts = false
variable = null
maxChars = null

Once you create a text field, you can use the TextFormat object to format the text. You must create a new TextFormat object and then pass it as a parameter to the setTextFormat method of the TextField object. A text field created with the createTextField method receives the following default TextFormat object:

font = "Times New Roman"
size = 12
textColor = 0x000000
bold = false
italic = false
underline = false
url = ""
target = ""
align = "left"
leftMargin = 0
rightMargin = 0
indent = 0
leading = 0
bullet = false
tabStops = [] (empty array)

To create a dynamic text field:

  1. Select a frame, button, or movie clip to which to assign the action.
  2. Select Window > Actions to open the Actions panel if it isn't already open.
  3. In the Actions toolbox, select the Objects category, then select the Movie category, then select the MovieClip category, then select the Methods category, then double-click the createTextField method.
  4. In the Object field, enter an instance name for the movie clip that will be the parent of the new text field. For this example, enter the alias _root because the main Timeline is the parent.
  5. Enter values for the following parameters:
  6. The following code displays in the Script pane:

    _root.createTextField("mytext",1,50,50,200,100);
    
  7. In the Actions toolbox, select the Objects category, then select the Movie category, then select the TextField category, then select the Properties category, then double-click text. For this example, enter myText in the Object parameter field.
  8. In the Value field, enter this is my first test field object text. The following text is displayed in the Script pane:
  9. mytext.text = "this is my first test field object text";
    

    This example creates a text field with an instance name myText, a depth of 1, a width of 200, a height of 100, an x value of 50, and a y value of 50.

For a detailed description of the createTextField method of the TextField object, see its entry in the online ActionScript Dictionary in the Help menu.