Return to Kroll Design home page

Flash Tutorials (with .FLA files included)

 
« Return

ActionScript 3

Formatting text using ActionScript

Create an instance of the text box using the TextField class. Create an instance of its text formatting using the TextFormat object. Text formatting parameters can be assigned when the TextFormat object is instantiated (Example 1), or afterwards (Example 2). Assign the TextFormat instance to the defaultTextFormat property of the text box BEFORE assigning text to the text property of the TextField, or it won't work properly. Then of course you need to do addChild to put the TextField on the stage. The TextFormat object has a bunch of properties, this example only uses the first four: font (font family), size (font size), color (font color), bold. To see the entire list, in the Actions Panel, position the cursor over "TextFormat" and hit F1.

Download the FLA file

/*
parm1 is font:String (font family)
parm2 is size:Object (font size)
parm3 is color:Object (font color)
parm4 is bold:Object (boolean indicating true or false for bold)
There are many more parms available, but those are the first four.
For more info, position cursor over "TextFormat" in code and hit F1.
*/

var abc1:TextField = new TextField();
var f1:TextFormat = new TextFormat("Trebuchet MS",30,0,true); 
abc1.defaultTextFormat = f1; 
abc1.text = "This is example 1"; // formatting must be done before you assign text
abc1.border = true;
abc1.x = 50;
abc1.y = 20;
abc1.height = 50;
abc1.width = 500;
addChild(abc1);

var abc2:TextField = new TextField();
var f2:TextFormat = new TextFormat(); 
f2.font = "Trebuchet MS";
f2.size = 30;
f2.color = 0;
f2.bold = true;
abc2.defaultTextFormat = f2; 
abc2.text = "This is example 2"; // formatting must be done before you assign text
abc2.border = true;
abc2.x = 50;
abc2.y = 120;
abc2.height = 50;
abc2.width = 500;
addChild(abc2);


 
« Return


©2012 Kroll Design    info@KrollDesign.net    781.910.3694
Last modified: 12/31/1969 7:00 PM