View comments | RSS feed

getTextExtent (TextFormat.getTextExtent method)

public getTextExtent(text:String, [width:Number]) : Object

Deprecated since Flash Player 8. There is no replacement.

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.

Availability: ActionScript 1.0; Flash Player 6 - The width parameter is supported in Flash Player 7.

Parameters

text:String - A string.

width:Number [optional] - A number that represents the width, in pixels, at which the specified text should wrap.

Returns

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

Example

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

var my_str:String = "Small string";

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

// Obtain metrics information for the text string
// with the specified formatting.
var metrics:Object = my_fmt.getTextExtent(my_str);

// Create a text field just large enough to display the text.
this.createTextField("my_txt", this.getNextHighestDepth(), 100, 100, metrics.textFieldWidth, 
metrics.textFieldHeight);
my_txt.border = true;
my_txt.wordWrap = true;
// Assign the same text string and TextFormat object to the my_txt object.
my_txt.text = my_str;
my_txt.setTextFormat(my_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 my_fmt:TextFormat = new TextFormat();
// Specify formatting properties for the TextFormat object:
my_fmt.font = "Arial";
my_fmt.bold = true;
my_fmt.leading = 4;

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

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

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

Version 8

Comments


mspres said on Sep 25, 2005 at 8:59 AM :
It is deprecated and "There is no replacement." I make VERY heavy use of this, please refer us to the new features that are available in Flash 8's text objects that can be used instead.
No screen name said on Oct 2, 2005 at 3:05 PM :
"There is no replacement!??"

There HAS to be some way to measure text. Come on Macromedia -- give us a hand...
Thais Derich said on Oct 3, 2005 at 10:02 AM :
Thank you for pointing this out. This is a mistake in the documentation. TextFormat.getTextExtent is NOT deprecated. I have updated the documentation source.
acrowne said on Oct 17, 2005 at 9:23 AM :
Can you spell out more clearly what features are supported by Flash Player 6? I haven't been able to get textFieldWidth or textFieldHeight to work in my Flash 6, AS2 document.
Thais Derich said on Nov 18, 2005 at 2:11 PM :
acrowne-
Thank you for your question. textFieldWidth is not supported in Flash 6. It is supported in Flash 7 and higher.
Ã…nubis said on Mar 7, 2006 at 7:35 AM :
When using the getTextExtent in Flash 8, I have found that there is a flaw in the dimensions output to getTextFieldHeight.

In testing, I was using Verdana (not embedded) at 10 point, with wrapping at 170px. In practice this did not function correctly and always returned incorrect height specifications.

When changing to Verdana 12 point, the issue appeard to correct its self as with changing to Arial.

When changing to Verdana (embedded) at 10 point it then functioned correctly. Is this a bug in the getTextExtent or am I missing something?
DRDELL said on Mar 25, 2006 at 3:00 PM :
I am at the point where I am rewriting some text formating code and I need to get confirmation that getTextExtent is NOT deprecated. I would also like to know how to obtain and embedd corrected documentation to my Flash Proffesional standalone Help System. I had always assumed that when I pressed the update button that a comparison with updated docs would be made but Derich's comment leads me to believe that this is not the case. So, if I cannot trust the docs, or the auto update docs, where are the actual current best docs kept and how to I get them into my standalone flash proffessional 8?
shimi2 said on Apr 4, 2006 at 10:28 AM :
Hi - Thais Derich's comment on Oct 3, 2005 stands.
getTextExtent() is in fact not deprecated in Flash 8. This was an unfortunate documentation error.
Next2Heaven said on Nov 13, 2006 at 11:00 AM :
When the embedded font and the format you specify with actionscript are different when it comes to the font size, it seems to return incorrect getTextFieldHeight and textFieldWidth variables.
No screen name said on Mar 8, 2007 at 7:35 PM :
I have encountered problem using getTextExtend with Mac Flash 8
IDE. After calling the method, it returned undefined and deleted all
movie clips that i created dynamically on the stage.

However, I could not confirm under what circumstances when the
method will fail, as I have been getting the method working in other
occassions.

Now I use TextField.autosize instead. It just work fine.
THE NEB said on Apr 26, 2007 at 1:27 PM :
I have encountered the same problem as "No screen name" (see previous comment). The movieclip was deleted when I used getTextExtend (on Mac Flash 8), but finally I found some conclusion about it and resolve my problem, but not the bug itself... except for this:

I found something in the help section of "getNextHighestDepth (MovieClip.getNextHighestDepth method)" :
Note: If you are using version 2 components, do not use this method. If you place a version 2 component either on the Stage or in the Library, the getNextHighestDepth() method can sometimes return depth 1048676, which is outside the valid range. If you are using version 2 components, you should always use the version 2 components DepthManager class.

The deleted movieClip was always the one on the depth 1048676, not another one (because I've got a version 2 components on the Stage and the deleted movieClip was created using getNextHighestDepth), so I found it was kind of my mistake. When I specify my own Depth, there's no movieclip deleted when calling getTextExtend.

Finally, Thais Derich you've said that you have updated the documentation source but it is still showing "Deprecated since Flash Player 8. There is no replacement." in the top of this page, what have you updated ???
tderich said on May 1, 2007 at 10:36 AM :
When a correction is made in the source, it is released with the next product version. Please see Flash 9: http://livedocs.adobe.com/flash/9.0/main/00002268.html

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/8/main/00002808.html