4 Replies Latest reply on Feb 8, 2007 10:53 AM by HunterZolomon

    getTextExtent?

    HunterZolomon
      I have a dynamic textfield with a specific width. Sometimes the text going into the field is too wide. What is the simplest way in AS3 to cut the string off and end it with 3 dots "..." to fit the textfield? Do I really need a loop to shave one letter off at a time? Sure I could specify number of chrs, but it's not pixel perfect. I believe getTextExtent was used before...
        • 1. Re: getTextExtent?
          jlingwai Level 1
          Assuming you are dynamically placing a string(strField1) into the textfield(txtField1), and the string length varries and you want any string that is over 10 char. to be cut off and display "..." at the end, but if its over 10 char you need to cut the string to 7char + "..." heres the AS3 code

          if (strField1.length > 10){
          txtField1.text = strField1.substring(0,6)+"...";
          }
          else{
          txtField1.text = strField1;
          }
          • 2. Re: getTextExtent?
            HunterZolomon Level 1
            Thanks but:
            "Sure I could specify number of chrs, but it's not pixel perfect."

            I need a simple function that shortens the string according to a set width. Actually calculating the exact pixelwidth of both String and textField. Just like getTextExtent used to do.
            Yes there are workarounds, some not so exact like basing it on chr number, or an awkward loop. I really wonder why this function has been removed from the AS3 library? And what people use in place?
            • 3. Re: getTextExtent?
              jlingwai Level 1
              I think the getCharBoundaries() will help you acheive this
              • 4. Re: getTextExtent?
                HunterZolomon Level 1
                text.width would give you the pixel width of a String too. But as far as I can tell, ány of these methods still needs to be looped in order to see which characters should be removed in order to fit the String in a textField with a specific width. But I suppose you have a point, the getCharBoundaries() is a great method for many text issues and probably the one I'll use.

                I appreciate you taking your time to discuss this with me.