So I make an instance of a display object defined in an external .swf that has a TextField. I set that TextField to a new format using setTextFormat(format) and then defaultTextFormat = format. When I set the TextField's htmlText property, the defaultTextFormat is suddenly reset to what it was originally.
So something like:
var view:MovieClip = new externalDefinition();
var label:TextField = view.label;
var format:TextFormat = label.getTextFormat();
format.fontName = "Century Gothic";
label.defaultTextFormat = format;
// Century Gothic
// <TEXTFORMAT LEADING="-1"><P ALIGN="LEFT"><FONT FACE="Century Gothic" SIZE="18" COLOR="#271F60" LETTERSPACING="0" KERNING="1">label</FONT></P></TEXTFORMAT>
label.htmlText = "<b>Hello!</b>"
// <TEXTFORMAT LEADING="-1"><P ALIGN="LEFT"><FONT FACE="Arial" SIZE="18" COLOR="#271F60" LETTERSPACING="0" KERNING="0"><B>Hello!</B></FONT></P></TEXTFORMAT>
So why does it do this? I understand why setting htmlText might reset the formatting to the default formatting but why does it change defaultTextFormat? Why doesn't it use the defaultTextFormat formatting I changed? Why does it use the original formatting the text field had when it was defined in asset library? How can I tell flash to use my formatting when I change htmlText? Where does it keep the original information stored and how does it revert back to it?
It does seem to revert to the prior default when setting htmlText - I quickly duplicate this problem. However, it seems to revert to the font the TextField was set to in the .FLA. Is there any reason you are trying to change this at runtime? Maybe you can just switch the font in the external .swf file since this does seem to be a Flash Player bug? Alternatively - just update the TextFormat whenever you set the htmlText property (that's a pain, I know - but just make a function to update the text and it won't be that bad)
I quickly scetched up an example. Yes, I mean font property. Sorry. I didn't compile the example, I am using it to demonstrate the problem I am having (partly to make it clear, partly because I was developing on a different computer and could not copy/paste). It seems that Nabren has confirmed the issue however.
And no, I cannot change the font in the .fla, I need to set it at runtime. And yes, I know I can change the font back afterwards, but that is a terrible solution. It is a large framework I am working on and affects many assets and it would be terrible to require anyone who uses the framework to do this themselves. If there is no other way I might have to some kind of scheme for doing this automatically, but I would like to avoid this.
I want to know why it reverts the TextField property and if there is any way that I can prevent it from doing so in the first place. Where is the original font even stored in the TextField? How does it even know what the original font is? If it is somehow possible to change the original data at runtime then that would be very good.
One more thing, I cannot use StyleSheets for these textfields easily. Maybe if there is no other solution I could try to refactor things and get this to work but right now it won't. So my question is specifically about setting the htmlText property in conjunction with defaultTextFormat.
According to the API this behaviour is -sad for you, I know- intended, thats at least what I read from this warning in the documentation:
Use the TextField.defaultTextFormat property to apply formatting BEFORE you add text to the TextField, and the setTextFormat() method to add formatting AFTER you add text to the TextField.
Maybe the adding/changing of text triggers the reset and the loosing of your defaultTextFormat. Maybe somehow you can empty the Textfield, store its content in a var, set the defaultTextFormat, set it to htmlText, and when you`re done fill the htmlText with your var?
I've read that and I am pretty sure that is not what is meant in the documentation.
They mean, if you change defaultTextFormat, then any new text added after it is set uses the defaultTextFormat, however text already set does not use it. If you use setTextFormat, current text is set to that new format, but any text added later still uses defaultTextFormat.
Basically setTextFormat is a one time thing, it applies to text that is currently in the TextField, and defaultTextFormat is, as the name implies, the default text formatting for any new text added.
That is what that warning is saying, and yes, that is intended behavior. But that is not the problem for me. I am appliyng the defaultTextFormat before I am changing the text, not after I am changing the text. However defaultTextFormat is being reset on the condition a) htmlText is used and b) the TextField came from a symbol in Flash Professional layout.
I think this is a bug and unrelated to that warning. I am always weary of calling things a bug in Flash but that appears to be what this is. :/
I tested this and can`t reproduce your problem:
var tf:TextField = new TextField();
//These two are Times New Roman (The system default)
tf.text = "HELLO WORLD";
tf.htmlText = "<b>HELLO WORLD</b>";
//All the following stay Arial, it never reverts back to Times New Roman
var arial:TextFormat = new TextFormat("Arial");
tf.defaultTextFormat = arial;
tf.text = "HELLO WORLD";
tf.htmlText = "<b>HELLO WORLD</b>"
//According to you at this point it should reset to Times New Roman, but it never does
What should I change to see the "wrong" defaultTextFormat resetting?
It happens when you are using a TextField that is part of an exported library symbol from Flash Professional.
If you want to reproduce the problem, you must create a new FLA file in Flash Professional. Make a library symbol. Add a TextField inside that library symbol. Give the text field an instance name like 'label', and some text. I am using _sans as the font face. Export the library symbol for actionscript in the symbol properties, give it a class name like "LabelExample". In the file's Publish Settings, set it to publish a .swc file. Publish it.
Now include the .swc file in a new project. You should be able to make an instance of LabelExample. Make a new instance, like:
var clip:MovieClip = new LabelExample();
var textField:TextField = clip.label;
Now apply a new defaultTextField to this text field. It will work as long as you only change the text property. Then try to change the htmlText property. Unlike a normal new instance of TextField, which will continue to apply your defaultTextFormat formatting to the text, even when htmlText is set, it will reset your defaultTextFormat to whatever it was originally when the symbol was published, and not use the defaultTextFormat you set. I don't have any idea why it would do this, it is inconsistent with how a TextField normally behaves and by all indication is supposed to behave. I believe this is a bug in the Flash framework. :/