Changing a text field's dimensions at runtime

You may need to get or set a text field's dimensions dynamically at runtime, rather than in the authoring environment. The next example creates a text field on a timeline and sets its initial dimensions to 100 pixels wide by 21 pixels high. Later, the text field is resized to 300 pixels wide by 200 pixels high, and it is repositioned to the center of the Stage.

To resize a text field using ActionScript:

  1. Create a new Flash document and save it as resizeText.fla.
  2. Add the following ActionScript to Frame 1 of the Timeline:
    this.createTextField("my_txt", 10, 0, 0, 100, 21);
    my_txt.border = true;
    my_txt.multiline = true;
    my_txt.text = "Hello world";
    my_txt.wordWrap = true;
    my_txt._width = 300;
    my_txt._height = 200;
    my_txt._x = (Stage.width - my_txt._width) / 2;
    my_txt._y = (Stage.height - my_txt._height) / 2;
    
  3. Save the Flash document and select Control > Test Movie to see the results in the authoring environment.

The previous example resized a dynamically created text field to 300 pixels by 200 pixels at runtime, but when you load content from an external website and are not sure how much content will be returned, this technique may not be suitable for your needs. Fortunately, Flash includes a TextField.autoSize property, which you can use to automatically resize a text field to fit its contents. The following example demonstrates how you can use the TextField.autoSize property to resize the text field after text is added to the text field.

To automatically resize text fields based on content:

  1. Create a new Flash document and save it as resizeTextAuto.fla.
  2. Add the following code to Frame 1 of the main Timeline:
    this.createTextField("my_txt", 10, 10, 10, 160, 120);
    my_txt.autoSize = "left";
    my_txt.border = true;
    my_txt.multiline = true;
    my_txt.text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
    my_txt.wordWrap = true;
    

    NOTE

    If you paste this code directly into the Actions panel from some versions of Flash Help, you may encounter line breaks in the long text string. In this case, the code won't compile. If you encounter this situation, enable Hidden Characters on the pop-up menu of the Actions panel, and then remove the line break characters in the long text string.

  3. Save the Flash document and select Control > Test Movie to view the Flash document in the authoring environment.

    Flash resizes the text field vertically so that all the content can be displayed without being cropped by the text field boundaries. If you set the my_txt.wordWrap property to false, the text field resizes horizontally to accommodate the text.

    To enforce a maximum height on the auto-sized text field (so that the text field height doesn't exceed the boundaries of the Stage), use the following code.

    if (my_txt._height > 160) {
        my_txt.autoSize = "none";
        my_txt._height = 160;
    }
    

You must add some scrolling functionality, such as a scroll bar, to allow users to view the remainder of the text. Alternatively, you can roll the mouse pointer over the text; this method is often adequate while testing this code.

For samples that demonstrate how to work with text fields using ActionScript, see the Flash Samples page at www.adobe.com/go/learn_fl_samples. Download the Samples zip file and navigate to the ActionScript 2.0/TextFields folder to access these samples: