Flash / dynamic text width calculator

Measuring the width of dynamic text

In Flash5, there is one oddity with dynamic text fields: you can't really tell the width of the text in box. In cases where you have to align some movieclip by the center of textbox there is no straightforward way to do it. The first idea may be putting the textbox in the movieclip and read its _width property. Alas, once the textbox is dynamic, the _width of wrapping moveclip is equal to the width of text's bounding box rather than text istelf. To deal with this, I have developed a class which calculated the width of given text.


Downloads

TextWidth.zip - AS, FLA, and SWF files of above example. See the class documentation in textwidth.as file. This ZIP also includes Branden Hall's string object rewrite for quicker processing.

Usage

To have class present in your flash application, just #include the textwidth.as file in the beginning of your movie. The documentation of classes can be found in the .as file.

How to adjust the calculator to your specific typeface

  1. Open the source TextWidth.fla
  2. Double-click on the "Measurer clip" to open the character set movieclip. Now you have a 228-frame movie clip, and you are at the first frame of it. There are two layers: "Top marker" and "Characters"
  3. Don't leave the first frame. Click on the "Edit multiple frames" button by the timeline. The frame range selector appears on the ruler.
  4. Pull the right handle of the range selector all over the timeline up to last frame. It could require number of steps because you can't see whole timeline at once.
  5. Now in the character area on stage you see all the chars smashed together in one pile. Carefully select all of them by dragging a rectangle over it. Note that you can see the frames from where you have selected content on the timeline. All frames must be highlighted.
  6. In the text properties, select the font face, style, and size.
  7. You can unselect the "edit multiple frames" option now.
  8. In the first frame of movie, you must paste the following code:
    var myCharSet = new charSet(this["TheNameOfTheMeasurerMovieclip"]);
    trace(charSet.getArrayString());
  9. Test movie. You will get the necessary array string traced in the output window, and you can use that in your projects without the need to recount the widths of characters every time the movie is run.