Call: (209) 560-6611

How to format the text in a text field in AS3

Date: December 17, 2014 Author: ang Category: Tutorials Tags: Comments: 0

Formatting a TextField object is different than formatting a TextArea object in AS3. Below is an example main project class that formats a text field in AS3.

 Javascript |  copy code |? 
01
02
package  {
03
 
04
	import flash.display.MovieClip;
05
	import flash.text.TextField;
06
    import flash.text.TextFieldAutoSize;
07
    import flash.text.TextFormat;
08
 
09
	public class main extends MovieClip {
10
 
11
		//create vars
12
		//create text field object
13
		var myTextField:TextField = new TextField();
14
		//create text field formatting style variable
15
		var myTextFormat:TextFormat = new TextFormat();
16
 
17
		//Note: text muse be added after the TextFormat instances is assigned to else the fomatting won't apply.
18
 
19
		public function main() {
20
			// constructor code
21
			addChild(myTextField);
22
			myTextField.x = 30;
23
			myTextField.y = 30;
24
			myTextField.width = 250;
25
			myTextField.height = 100;
26
			//myTextField.text = "Hello World";
27
			//set style formating variable
28
			myTextFormat.font = "Arial";
29
			myTextFormat.size = 40;
30
			myTextFormat.bold = true;
31
			myTextFormat.italic = true;
32
			myTextFormat.underline = true;
33
			myTextFormat.color = 0xef0000;
34
			//apply style to textField variable
35
			myTextField.defaultTextFormat = myTextFormat;
36
			myTextField.text = "Hello World";
37
 
38
		}
39
 
40
 
41
	}
42
}
43
 
44

Here is another example on how to format a TextField, and the text in the TextField, from Adobe's website.

Leave a Reply

Your email address will not be published. Required fields are marked *

Before you post, please prove you are sentient.

What is 5 * 5?

Before you post, please prove you are sentient.

what is 6 plus 2?