Setting Text Font and Size

Setting Text Font and Size

Flash CS4 ActionScript 3.0 Tutorial

 

How to Change the Flash Text Font and Size with ActionScript

 

 

Input text and Dynamic text can be incorporated into ActionScript. You can write code to alter the font or size of the text. For more information about other text subjects, see topics, Dynamic Text, Input Text, Inputting Values and Displaying Results, and Setting Text Color and Text Background Color.

In this example we will use an Input text field to demonstrate the process.

Tutorial Elements

Input Text box with instance name of inText_txt. Enter the word Flash in the text box

 

Step One

In a new Flash document, add a new layer. Name the upper layer Actions and the lower layer Content.

Step Two

Click the keyframe for the Content layer.

Step Three

Add an empty Input text block. Choose Input text In the Properties panel.

Step Four

Double-click the text box and type the word Flash in the text box.

Step Five

Click the Input text box and assign it an instance name of inText_txt in the Properties panel.

Step Six

Option double-click (PC - Alt double-click) the keyframe of the Actions layer to open the ActionScript panel.

Step Seven

Copy/Paste the following code to the ActionScript panel.

var changeText:TextFormat = new TextFormat();

changeText.font = "Arial";

changeText.size = 30;

changeText.bold = true;

inText_txt.setTextFormat(changeText);

Step Eight

Click the Auto Format button to format the code

Step Nine

Test your movie by pressing Cmd/Return (PC - Ctrl/Enter). The text font, font size, and bold setting have been altered by the ActionScript.

Dissecting the Code

The first line adds a new TextFormat variable named changeText.

The next line changes the font to Arial. Note the capital A in Arial. Line three sets the font size to 30 and the next line makes the text bold.

Line three assigns the changeText variable value to the Input text.

Try Some Variations

By changing the font, font size and bold settings you can alter the text object quite easily. Experiment with the settings to alter your text appearance.