Skip navigation
Currently Being Moderated

Changing defaultTextFormat then setting htmlText changes defaultTextFormat back?

Feb 13, 2013 8:08 AM

Tags: #textfeld #htmltext #text_formatting

Hello,

 

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;

 

trace(label.defaultTextFormat.fontName)

// Arial

 

var format:TextFormat = label.getTextFormat();

format.fontName = "Century Gothic";

label.setTextFormat(format);

label.defaultTextFormat = format;

 

trace(label.defaultTextFormat.fontName)

// Century Gothic

 

trace(label.htmlText)

// <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>"

 

trace(label.defaultTextFormat.fontName)

// Arial

 

trace(label.htmlText);

// <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?

 

Thanks,

Erik

 
Replies
  • Currently Being Moderated
    Feb 13, 2013 9:52 AM   in reply to Thorjelly

    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)

     
    |
    Mark as:
  • Currently Being Moderated
    Feb 13, 2013 9:54 AM   in reply to Thorjelly

    With that code you should get a bunch of errors.

    Neither TextFormat nor DefaultTextFormat has a Property fontName

    only Font has a property fontName.

     
    |
    Mark as:
  • Currently Being Moderated
    Feb 13, 2013 10:45 AM   in reply to Thorjelly

    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?

     
    |
    Mark as:
  • Currently Being Moderated
    Feb 13, 2013 1:23 PM   in reply to Thorjelly

    I tested this and can`t reproduce your problem:

     

    import flash.text.TextField;

    import flash.text.TextFormat;

     

    var tf:TextField = new TextField();

    addChild(tf);

    //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

    tf.setTextFormat(tf.defaultTextFormat);

     

    What should I change to see the "wrong" defaultTextFormat resetting?

     
    |
    Mark as:
  • Currently Being Moderated
    Feb 13, 2013 4:48 PM   in reply to moccamaximum

    It seems the TextField must be created in the timeline - not ActionScript - for this bug to happen.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points