TextFormatting in ActionScript 3 (setTextFormat)

  October 4, 2012   by:  muni   No Comment

The setTextFormat() method in actionScript 2 used to set the font properties to the text fields.

example:

var txt_format:TextFormat = new TextFormat();//TextFormat class creates new instances of it.

this.createTextField("txt_field", this.getNextHighestDepth(), 0, 0, 180, 80);//creates new text field

txt_format.color = 0x00FF00;
txt_format.size = 20;       //set the text format properties
txt_format.font = "Verdana";

txt_field.selectable = false;
txt_field.multiline = true;
txt_field.autoSize = true;
txt_field.setTextFormat(txt_format);//setTextFormat method set text format properties to the text field
txt_field.text = "Formatting Text in ActionScript 3";

where in the above coding setTextFormat() method sets color, font type and size to the text field.

But in ActionScript 3 setTextFormat() method is no longer exists to set text formats to the Text Field (both dynamic text field and static text field). So ActionScript 3 we are going to set text formats to the text Fields using TextFormat Class. In this tutorial we are going to use both Text Field Class and TextFormat Class to set font type, font color, position and the font size.

Here is the example code……..

In ActionScript 3 defaultTextFormat sets text format to the text Field.

txt_field.defaultTextFormat=txt_format;

var txt_field:TextField = new TextField();
var txt_format:TextFormat = new TextFormat();

addChild(txt_field);

txt_format.font = "Verdana";
txt_format.size = 25;
txt_format.color = 0x0000FF;
txt_format.leftMargin = 30;

txt_field.defaultTextFormat=txt_format; 

txt_field.border = true;
txt_field.x = 50;
txt_field.y = 25;
txt_field.autoSize = TextFieldAutoSize.LEFT;
txt_field.text = "Formatting Text in ActionScript 3";
setTextFormat In ActionScript 3