TextFormat.getTextExtent() is All Right in My Book

ActionScript 2.0

As of this writing, the Flash 8 ActionScript 2.0 Language Reference incorrectly states that the TextFormat.getTextExtent() method is deprecated.  It makes this gaffe twice:  in the method entry itself and in a separate “Deprecated Function summary” listing.  This occurs in both the onboard Help and the LiveDocs version.  On September 20, 2005 (nearly a year ago), Adobe editor Thais Derich acknowledged this error and stated that the documentation source has been updated, which is good news — but for whatever reason, this update still hasn’t been made available to the public (for example, shouldn’t the Update button in the Actions panel correct this?).

Don’t get me wrong, I’m not grouching.  ;)   The Language Reference is a lengthy document indeed, and the lion’s share is comprised of useful (and correct!) information.  But I figured I’d mention this, because the method comes in handy from time to time.  You may safely use this method in ActionScript 2.0 without feeling icky.

So, what does it do?  TextFormat.getTextExtent() allows you to determine the theoretical pixel dimensions of a string — as if that string were formatted in a given font, weight, size, and more.  In other words, it allows you to test for, say, the width of a text field in a certain styling without your having to create the text field first.

Thanks to my Adobe newsgroup compadre Marc Kassay for pointing this issue out to me.

6 Responses to “TextFormat.getTextExtent() is All Right in My Book”

  1. Davel_x Says:

    Does it also work for the height of a textField ? This would be very useful :)

  2. David Stiller Says:

    According to the docs, TextFormat.getTextExtent() returns “An object with the properties width, height, ascent, descent, textFieldHeight, textFieldWidth.” It’s pretty informative! :)

  3. Davel_x Says:

    I thought it would be quite ‘ugly’ to test if a text fits in an area by adding small parts of the text. :)
    It will be using this tip soon, thank you !!

  4. Paul Cripps Says:

    Macromedia!! Nice… great functionality, hopefully it will stay in the future too.

  5. Rothrock Says:

    I’ve always been a big fan of getTextExtent() as was quite alarmed when it was “depreciated.”

    But recently, I’ve found it to be unreliable. I’m using an embedded font, but it makes a difference if the font is active/available on the host computer or not. For example, the word “QUACKING” is reported as being 76 pixels wide if the font is available, but 62 if it isn’t.

    In both cases the embedded font renders properly. And I measured the dimensions with photoshop and they are both the correct 76 pixels wide.

    I’m reporting this to Adobe, but David “Seething Cauldron” Stiller’s blog was one of the top google hits as I was trying to do more research!

  6. David Stiller Says:

    Rothrock,

    Haha, thanks for the tip!

Leave a Reply