TextFormat.getTextExtent()

Availability

Flash Player 6. The optional width parameter is supported in Flash Player 7.

Usage

my_fmt.getTextExtent(text, [width])

Parameters

text A string.

width An optional number that represents the width, in pixels, at which the specified text should wrap.

Returns

An object with the properties width, height, ascent, descent, textFieldHeight, textFieldWidth.

Description

Method; returns text measurement information for the text string text in the format specified by my_fmt. The text string is treated as plain text (not HTML).

The method returns an object with six properties: ascent, descent, width, height, textFieldHeight, and textFieldWidth. All measurements are in pixels.

If a width parameter is specified, word wrapping is applied to the specified text. This lets you determine the height at which a text box shows all of the specified text.

The ascent and descent measurements provide, respectively, the distance above and below the baseline for a line of text. The baseline for the first line of text is positioned at the text field's origin plus its ascent measurement.

The width and height measurements provide the width and height of the text string. The textFieldHeight and textFieldWidth measurements provide the height and width required for a text field object to display the entire text string. Text fields have a 2-pixel-wide "gutter" around them, so the value of textFieldHeight is equal the value of height + 4; likewise, the value of textFieldWidth is always equal to the value of width + 4.

If you are creating a text field based on the text metrics, use textFieldHeight rather than height and textFieldWidth rather than width.

The following figure illustrates these measurements.

When setting up your TextFormat object, set all the attributes exactly as they will be set for the creation of the text field, including font name, font size, and leading. The default value for leading is 2.

Example

This example creates a single-line text field that's just big enough to display a text string using the specified formatting.

var text = "Small string";

// Create a TextFormat object,
// and apply its properties.
var txt_fmt = new TextFormat();
with(txt_fmt) {
  font = "Arial";
  bold = true;
}

// Obtain metrics information for the text string
// with the specified formatting.
var metrics = txt_fmt.getTextExtent(text);

// Create a text field just large enough to display the text.
this.createTextField ("textField", 0, 100, 100, metrics.textFieldWidth, metrics.textFieldHeight);
textField.border = true;
textField.wordWrap = true;
// Assign the same text string and
// TextFormat object to the TextField object.
textField.text = text;
textField.setTextFormat(txt_fmt);

The following example creates a multiline, 100-pixel-wide text field that's high enough to display a string with the specified formatting.

// Create a TextFormat object.
var txt_fmt:TextFormat= new TextFormat();

// Specify formatting properties for the TextFormat object:
txt_fmt.font = "Arial";
txt_fmt.bold = true;
txt_fmt.leading = 4;

// The string of text to be displayed
var textToDisplay:String = "Macromedia Flash 7, now with improved text metrics.";

// Obtain text measurement information for the string,
// wrapped at 100 pixels.
var metrics:Object = txt_fmt.getTextExtent(textToDisplay, 100);

// Create a new TextField object using the metric
// information just obtained.
this.createTextField ("textField", 0, 50, 50-metrics.ascent, 100, metrics.textFieldHeight)
textField.wordWrap = true;
textField.border = true;
// Assign the text and the TextFormat object to the TextObject:
textField.text = textToDisplay;
textField.setTextFormat(aformat);