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);
TextFormat.getTextExtent()
721
Need help?
Do you have a question about the FLASH MX 2004 - ACTIONSCRIPT and is the answer not in the manual?
Questions and answers