4 Replies Latest reply on Aug 15, 2008 7:53 AM by Newsgroup_User

    Measure Width of a letter in Dynamic Text Box (k)

    Level 7
      Is there a way to measure the width of a letter in a a dynamic text box,
      not the width of the text box itself?

      I have some ActionScript I found online
      http://www.kirupa.com/developer/actionscript/ascript_text_animation.htm

      that does a nice job of animating text, but if you choose another type
      face the kerning looks awful. I need to measure the width of the
      dynamically generated letters in a text box, not the width of the text
      box itself.

      Is this possible in ActionScript 2.0?

      I found this for ActionScript 3.0:
      http://injun.ru/flash10api/flash/text/TextLineMetrics.html



      -Kirk
        • 1. Re: Measure Width of a letter in Dynamic Text Box (k)
          Level 7
          Or is there a way to "shrinkwrap" the dynamic textbox the letter sits in?

          I've been trying to do this using AutoSize, but I am obviously not using
          it correctly.

          -Kirk


          W. Kirk Lutz wrote:
          > Is there a way to measure the width of a letter in a a dynamic text box,
          > not the width of the text box itself?
          >
          > I have some ActionScript I found online
          > http://www.kirupa.com/developer/actionscript/ascript_text_animation.htm
          >
          > that does a nice job of animating text, but if you choose another type
          > face the kerning looks awful. I need to measure the width of the
          > dynamically generated letters in a text box, not the width of the text
          > box itself.
          >
          > Is this possible in ActionScript 2.0?
          >
          > I found this for ActionScript 3.0:
          > http://injun.ru/flash10api/flash/text/TextLineMetrics.html
          >
          >
          >
          > -Kirk
          • 2. Re: Measure Width of a letter in Dynamic Text Box (k)
            Level 7
            Kirk,

            >> Is there a way to measure the width of a letter in a a dynamic
            >> text box, not the width of the text box itself?

            There is. You're after the TextFormat.getTextExtent() method. Give
            that a shot in the ActionScript 2.0 Language Reference (there are useful
            examples in its listing), and I'll be happy to answer specific questions for
            you, if you get stuck. The most interesting thing about this particular
            method -- at least, it's interesting to me -- is that you can measure the
            width of any letter (or full sentence; whatever string you like) even
            without going to the expense of creating or using a text field. You just
            create a TextFormat instance, configure it with the font settings you desire
            (font family, font size, bold, etc.) and feed it whatever string you please.
            The method gives you back an object with properties that describe a handful
            of values that are not specific to any text field.

            > Or is there a way to "shrinkwrap" the dynamic textbox the letter
            > sits in?
            >
            > I've been trying to do this using AutoSize, but I am obviously not
            > using it correctly.

            Let's see some of your code. The TextField.autoSize property certainly
            gives you what might be called "shrink wrap," but even so, text fields are
            always a tad wider than the text they contain.


            David Stiller
            Co-author, Foundation Flash CS3 for Designers
            http://tinyurl.com/2k29mj
            "Luck is the residue of good design."


            • 3. Re: Measure Width of a letter in Dynamic Text Box (k)
              xparrot_dude Level 1
              I have changed the ActionScript from the example so that the text field is dynamically generated. It appears to work, the text widths are different. But no matter what I do the kerning still sucks.

              // import tween classes
              import mx.transitions.Tween;
              import mx.transitions.easing.*;
              //
              ii++;
              char.duplicateMovieClip("char"+ii, ii);
              _root["char"+ii].createTextField("true_txt", 997+ii, 0, 0, 1, 1);
              _root["char"+ii].true_txt.autoSize = true;
              _root["char"+ii].true_txt.text = text.substr(ii-1, 1);
              trace("char width= "+_root["char"+ii]._width);
              trace(_root["char"+ii].true_txt.text);
              trace(_root["char"+ii]._width);
              widthKeeper = widthKeeper+_root["char"+ii]._width;
              //
              var Animation:Object;
              //
              Animation = new Tween(_root["char"+ii], "_alpha", Regular.easeInOut, 0, 100, 1, true);
              Animation = new Tween(_root["char"+ii], "_xscale", Regular.easeInOut, 400, 100, .5, true);
              Animation = new Tween(_root["char"+ii], "_yscale", Regular.easeInOut, 400, 100, .5, true);
              Animation = new Tween(_root["char"+ii], "_x", Regular.easeInOut, widthKeeper-100, widthKeeper/2, .5, true);
              Animation = new Tween(_root["char"+ii], "_y", Regular.easeInOut, 500, nYstart+nLNumber*nLSpace, .5, true);

              • 4. Re: Measure Width of a letter in Dynamic Text Box (k)
                Level 7
                Wow, that was complicated, but I got it working. Had to store the width
                of the previous letter and use that for spacing. Was originally using
                the current letter for the width which was throwing everything off.

                Do have one, small hurdle left.

                When I choose the font, color, size in ActionScript, it works. But it
                also chops off the top of the letter.

                I've tried several things but they are not working.

                Is there a vertical align for dynamically generated text fields? Or a
                way to say, make the dynamic text field a certian height? Or have the
                text align to the center vertically or baseline?

                -Kirk



                xparrot_dude wrote:
                > I have changed the ActionScript from the example so that the text field is
                > dynamically generated. It appears to work, the text widths are different. But
                > no matter what I do the kerning still sucks.
                >
                > // import tween classes
                > import mx.transitions.Tween;
                > import mx.transitions.easing.*;
                > //
                > ii++;
                > char.duplicateMovieClip("char"+ii, ii);
                > _root["char"+ii].createTextField("true_txt", 997+ii, 0, 0, 1, 1);
                > _root["char"+ii].true_txt.autoSize = true;
                > _root["char"+ii].true_txt.text = text.substr(ii-1, 1);
                > trace("char width= "+_root["char"+ii]._width);
                > trace(_root["char"+ii].true_txt.text);
                > trace(_root["char"+ii]._width);
                > widthKeeper = widthKeeper+_root["char"+ii]._width;
                > //
                > var Animation:Object;
                > //
                > Animation = new Tween(_root["char"+ii], "_alpha", Regular.easeInOut, 0, 100,
                > 1, true);
                > Animation = new Tween(_root["char"+ii], "_xscale", Regular.easeInOut, 400,
                > 100, .5, true);
                > Animation = new Tween(_root["char"+ii], "_yscale", Regular.easeInOut, 400,
                > 100, .5, true);
                > Animation = new Tween(_root["char"+ii], "_x", Regular.easeInOut,
                > widthKeeper-100, widthKeeper/2, .5, true);
                > Animation = new Tween(_root["char"+ii], "_y", Regular.easeInOut, 500,
                > nYstart+nLNumber*nLSpace, .5, true);
                >
                >
                >