Flash: Timeline dynamic TextFields losing formatting with new content?

You may notice that dynamic TextFields originating from the timeline occasionally maintain their formatting and sometimes lose it, even with programmatic fixes. This is because the problem lies in two different places, and this is how to fix it.

First, it is important to know that Flash will reset the formatting for any new text to that TextField’s defaultTextFormat. If you do not set this property you will have to setTextFormat after every time you change the text. Otherwise you will be stuck with defaults that you probably didn’t want. 

Most problems will be solved with this code (and it’s what most people searching Google are probably looking for). I made this static utility method to do all of my timeline TextField formatting to save time and clutter.

public static function formatTextfield(textField:TextField, label:String, html:Boolean = false, align:String = "left"):void {

 var fmt:TextFormat = textField.getTextFormat();  textField.defaultTextFormat = fmt;  textField.autoSize = align;

 if(html){ textField.htmlText = label; } else { textField.text = label; }

Here’s the rundown. Collect the TextFormat data before you change anything. Then you set the default to the existing format to take care of any future changes. Then do your text. The end. 

Now this will fix most people’s problems. However, there is another behavior in Flash that you need to be aware of. If there is text with more than one type of formatting in the TextField, Flash will revert to its defaults (which will lose all of your kerning, for example). Also, if there is no text in the field, it will also revert to defaults. I like putting a simple space in my TextFields to keep them looking clean, but as long as you keep it simple (one character is safest), you should be in good shape.

Note that if you are someone who is doing runtime font loading, you do not want to be putting any text from your primary font on the timeline or things can get all sorts of wonky. In this case you’d probably want to do your formatting then switch to a default font and reset the font programmatically.

Leave a Reply