AS3 > Use different text styles (bold and regular) in the same dynamic text field

actionscript-3flashfontstextfield

I've been using as3 for a lot of years but everytime I need to manipulate fonts I get crazy 🙁

My setup:
I'm filling up via code a movieClip with a lot of dynamic textFields. Their values come from an external XML.

My problem:
my client wants to insert html tags inside of the xml to have bold text in part of them. For example they want to have: "this string with this part bold".
The Xml part is ok, formatted with CDATA and so on. If I trace the value coming from the xml is html, but it is shown as regular text inside the textfield….

The textfields are using client custom font (not system font), and have the font embedded via embedding dialog panel in Flash by the graphic designer.

Any help?


This is the part of code that fills up the textfields (is inside a for loop)

  var labelToWrite:String = labelsData.label.(@id == nameOfChildren)[VarHolder.activeLang];
    if (labelToWrite != "") {
        foundTextField.htmlText = labelToWrite; 
        // trace ("labelToWrite is -->" +labelToWrite);
    }

And the trace outputs me

 This should be <b>bold text with b tag</b> and this should be <strong>strong text </strong>.

Best Answer

Your code looks good. So the issue will be with the embedded Font. When you embed a font in flash, it doesn't embed any separate bold versions, so you may need to embed a bold version of your font.

Some resources: Flash CS4 <b> tag in with htmlText

I find that html text works best by using the style sheets to declare your fonts instead of text formats.

           var style:StyleSheet = new StyleSheet();
               style.setStyle(".mainFont", { fontFamily: "Verdana", fontSize: 50, color: "#ff0000"            } );
           var foundTextField:TextField = new TextField();
           foundTextField.embedFonts = true;
           foundTextField.autoSize = TextFieldAutoSize.LEFT;
           foundTextField.antiAliasType = AntiAliasType.ADVANCED;
           foundTextField.styleSheet = style;
           foundTextField.htmlText = "<div class=\"mainFont\">" + labelToWrite + "</div>";

See: flash as3 xml cdata bold tags rendered in htmlText with an embedded font

Other reasons maybe: Are you embedding the right type of font (TLF vs CFF)? If not using the flash IDE to make your textField, are you registering the font?

Font.registerFont(MyFontClass);
Related Topic