Perhaps someone else has run into this issue and -may- know what is going on!
So this only happens with text fields that are set directly on the stage at author time. Not text fields that are dynamically created through code. While yes, creating them through code is an awesome solution sometimes that’s not really an option for me.
import flash.text.*;
// txt is a text field created on the stage with it's font set to "Verdana"
var txtFormat:TextFormat = new TextFormat("Times New Roman");
txt.defaultTextFormat = txtFormat;
trace(txt.defaultTextFormat.font); // traces Times New Roman
txt.htmlText = "Hello World"; // resets the TextFormat
trace(txt.defaultTextFormat.font); // traces Verdana
As you can see after setting the defaultTextFormat if you use .htmlText again it wipes out the text format completely.
Anyone know if there is a solution to this? I figure it might just be because the text field is on the stage.
No, it’s because HTML formatting is done by using series of TextFormats… so, if you need it to be Times, I’d wrap the text with “<font face=“Times New Roman”>Hello World</font>”. Or used StyleSheets like “.times{font-family: Times New Roman;}” + <span class=“times”>Hello World</span> etc.
Actually that’s not really correct. If you dynamically create the text field it maintains it’s default text format whenever you set htmlText. I’m just curious to see if anyone really knows why it resets it back to the stage defaultTextFormat when it’s placed on the timeline in flash.
This means the field on the stage will allready have some textformats that are used to render different tags, so that when you set default text format to it, it’ll apply only to the text that is outside the tags (i.e. if you’d used TextField.text instead of TextField.htmlText to set the text, it’d worked as you wanted it to).
Sorry, I admit I didn’t make it clear in my first post.
WHy are you using TextFormat for a htmlText.
htmlText should only be used with a styleSheet.
Just define your TextFormat in a styleSheet and apply it to the textfield.
It works great