Erratic behavior with Flash’s textWidth property

Just a quick post in case anyone else ever runs into this and manages to find their way to my blog.

I just came across this in a client project I’m working on. I was using a TextField in Flash CS5, and needed to know the width of the actual text inside it for positioning purposes. What I found was that in about 50% of cases, the textWidth property would return zero instead of the strings length in pixels. I tried using autoSize on the TextField and found that it would shorten half of its instances to a single character or less, causing the rest of the line to be invisible.

At first I thought there was something wrong with the font file, but the same happened with Verdana. So I started randomly changing settings, and it turns out that the “anti-alias for readability” setting was causing this. As soon as I changed that setting to “anti-alias for readability” the textfields were being autosized properly, and reported their actual lengths through the textWidth property.

I didn’t really investigate this any further once I found out this workaround. In fact, I needed to use “anti-alias for animation” anyway in my project. Please feel free to comment if this has happened to you, and/or you have more details about this strange little bug.

Roy | November 25, 2010 | English,Flash,Programming | Comments (10)
Tags: , , ,

10 Comments

  1. Link: The Finest Possibility for You, When Asperger's Diagnosis is Conclusive helpwithaspergers.com - helpwithaspergers.com
  2. got the same problem right now, the textwidth feature seems to be buggy, its not only the “anti-alias feature, this also happens with text-align center… the recognized textwidth-size seems to be 16/20 to the original :-/

    Comment by maemaemae — December 12, 2010 @ 9:47 am

  3. I have ran into this Problem several times and I have always had to call a buddy to come and fix it and he never told me how to fix it. So now I am looking forward to it happening again so i can call him to come over and when he gets there i will fix it in front of him. Thank you for the help!!

    P.S. I may take the liberty and time to investigate this a little futher with adobe support. And i will relay anymore information that i find back to you.

    Comment by Baby Gates — December 25, 2010 @ 7:30 am

  4. I may take the liberty and time to investigate this a little futher with adobe support. And i will relay anymore information that i find back to you.

    Comment by 增高药 — December 31, 2010 @ 4:39 pm

  5. Hi, I have the same problem.. It happens in a browser (most in FF3, sometimes GoogleChrome) not in standalone flashplayer. I dont know what to do.. sometimes multiline textfield dont wrap text, sometimes yes and it is ok – in the same “info box” component, but different instances. Some help? I cant find anything on the net. Thx

    Comment by cronello — April 7, 2011 @ 8:00 pm

  6. Thanks a lot! After a few hours of testing, this solution solved the problem!

    Comment by Victor — May 9, 2011 @ 5:07 am

  7. I had same when use statics property, set property multiline it ok

    Comment by Friv — August 30, 2011 @ 11:57 am

  8. you can get TextField props after TextField rendered just listen TextField object and handle Event.RENDER

    Comment by zZeke — April 6, 2012 @ 2:05 pm

  9. If you are getting the TextWidth of a Textfield also make sure you have your TextField scaled to 100%.

    It will only return the TextWidth of the font scaled at 100%, no matter what the scale.

    Earlier Flash versions have trouble displaying fonts over 90px, so you can maybe try setting the size in AS or using TLF textfields.

    Comment by Nunnsey — April 20, 2012 @ 4:42 pm

  10. Hello. I had the same problem. It isn’t important how long the text is. I had long sencentes and it also failed. But I foun a solution. If you are using “anti-alias for readability” you have to do this:

    textField.gridFitType = GridFitType.SUBPIXEL;

    Now it is working to me.

    Comment by Albert Llado — May 16, 2012 @ 4:08 pm