[AS3] Dynamically adding styles to TextField Component with TextFormat class in Flash using ActionScript


In AS3, styling the text content dynamically is done using TextFormat class.

Create a new Flash AS3 file, In the ActionPanelCreate a TextFormat object with the specified properties. You can then change the properties of the TextFormat object to change the formatting of text fields.

For making TextField Bold

var boldText:TextFormat = new TextFormat();
boldText.bold = true;

Setting format of TextField back to Normal

var normalText:TextFormat = new TextFormat();
normalText.bold = false;

Available Properties in TextFormat Class

Following are all the available properties in the TextFormat class. Please note that any parameter may be set to null, If you leave any parameters, they will be treated as null.

//Creates a TextFormat object 
var defaultFormat:TextFormat = new TextFormat();
//set the properties like this
defaultFormat.color = 0xFF0000;
defaultFormat.font = "Tahoma";
defaultFormat.bold = true;
defaultFormat.underline = true;
defaultFormat.bullet = true;
defaultFormat.align = TextFormatAlign.RIGHT;
defaultFormat.blockIndent = 15;
defaultFormat.indent = 10;
defaultFormat.italic = true;
defaultFormat.leading = 5;
defaultFormat.leftMargin = 10;
defaultFormat.letterSpacing = 2;
defaultFormat.rightMargin = 10;
defaultFormat.size = 15;
defaultFormat.url = "http://www.website.com/";
defaultFormat.target = "_blank";
defaultFormat.tabStops = [100,250];
// set the styles to your text field like this

Hope that helps.