Scaling dynamic text in AS3

Recently I’ve had the need to scale a dynamic text field which was fed by XML. Readability after scaling was not important, but it did have to look good (it’s for a full Flash site). However, scaling the MovieClip that the text field was in would cause some pretty chaotic behavior with the text — the text would try to scale with its parent but in doing so would shift around spastically until the parent MC settled into a new scale. This was unacceptable.

Click to view

A quick google didn’t yield anything relevant, so I was left to my own devices. I remembered being able to draw pixels to a BitmapData object while writing my game in order to improve performance, so I figured I’d see if I could just copy the pixels from the text field and “bake” it into a bitmap.

Turns out you can. In fact, Adobe had already done this in the help files and it’s actually extremely easy. Check it out:

?View Code ACTIONSCRIPT3
var tf:TextField = new TextField();
tf.text = "bitmap text";
var myBitmapData:BitmapData = new BitmapData(80, 20);
myBitmapData.draw(tf);
var bmp:Bitmap = new Bitmap(myBitmapData);
this.addChild(bmp);

No sweat right? Anyway, since I didn’t find any help on this anywhere else (granted, I didn’t look very hard), I thought I’d share this little adventure of mine. Hope it’s helpful.

download source

Tags: , ,

  • Twitter
  • del.icio.us
  • StumbleUpon
  • Facebook
  • Google Bookmarks
  • FriendFeed

13 responses to “Scaling dynamic text in AS3”

  1. OhCarson said:

    I don’t normally type in alll caps but THANK YOU. I needed this.

  2. Joy said:

    Cool stuff :)

  3. Jay said:

    This is a LIFE saver. We couldn’t find this anywhere (I guess it is all about what words you use to search for it). Thanks for the help!

  4. John S. said:

    Epic. I JUST ran into this problem tonight. Thank you for sharing, you’ve saved me countless hours of frustration! :D

  5. John S. said:

    Out of curiosity, were you able to apply any styling to the BitmapData before applying it to the Bitmap? I’m not successful doing so just yet, and having plain ol’ bitmap fonts isn’t desirable, aesthetically.

  6. Corey said:

    John: Aye. I’ve slapped a TextFormat and even some HTML onto the text object before it was rendered into a bitmap. If it helps, I’ll share my BitmapText class — feed it (width, height[, multiline:Boolean, format:TextFormat, avgPointSize:int, padding:int]), use addText(string) then render() will return a Bitmap object with everything. Looks complicated in retrospect, but it saved me some time anyway :shrug:

    There’s more detailed instructions in the class itself, and don’t forget to put it under com.coldconstructs.graphics

  7. Jeremy said:

    This is EXACTLY what I was looking for! Thanks so much!

  8. russdogg said:

    I believe embedding the fonts fixed this for me, when I had this issue before. Have to go look through some code to be sure.

  9. Lee said:

    Hello,

    This is not about textfields. It is about the cool little Flash navigation you have. How to did you get it to respond without having to be clicked on first? Anytime I embed a Flash file in a web page, you have to click on it before you can use it.

    thanks,
    lee

  10. Corey said:

    russdogg: I tried embedding the font but that didn’t seem to solve anything. Let me know if you get it working though, I’d like to know how you did it.

    Lee: I use SWFobject to embed all of my Flash thingies, and that’s what overcomes that little click-to-activate “feature” some browsers use.

  11. Robbieuitos said:

    Thanks! I’ve asked our friend Google about this for 5 hours. So thanks a lot!

  12. Erwan said:

    Thanks a lot!! Works great…

  13. scott said:

    Instead of turning your text into a bitmap (which will still look terrible when scaled to a larger size) you could have just turned the anti-alias from readability to animation. This solves this problem in most cases.

Leave a Reply