Formatting Text


You can apply formatting to text using the TextFormat class, which defines many properties that allow you to set everything from font family and font size to kerning and color. The Flash Help documentation lists all the properties of the TextFormat class, and it is an excellent reference for details of each setting. However, regardless of which properties you want to set, you'll construct the new TextFormat and apply it in the same ways.

When you want to construct a TextFormat object, you can use the constructor as part of a new statement, as the following code illustrates:

   var formatter:TextFormat = new TextFormat();


After you construct a TextFormat object, you can apply settings. The following sets the font and size properties:

   formatter.font = "_sans";    formatter.size = 20;


After you set the properties, you can apply the formatting to a text field with the setTextFormat() method of the TextField class. The method allows you to apply a TextFormat object to the entire text field. The following code illustrates by applying a TextFormat object called formatter to the text of a text field called exampleField:

   exampleField.setTextFormat(formatter);


When you apply formatting to a text field with the setTextFormat() method, it applies the current TextFormat settings to the current text of the text field. If you update the text after applying the formatting, you'll have to reapply the formatting. And if you change the settings of the TextFormat object, you'll have to reapply it if you want it to change the formatting of the text field.

You can optionally apply the formatting to just a specific substring of the text by specifying the starting and stopping indices of that substring as parameters for the setTextFormat() method.

   exampleField.setTextFormat(formatter, 10, 20);


In the next task, you'll add a message when the user wins the game. The message uses a TextFormat object.

1.

Open the Flash document you completed in the previous task. Optionally, you can open memory5.fla from the Lesson08/Completed directory.

The Flash document contains all the necessary assets and code as the starting point for this task.

2.

Select the first keyframe of the main Timeline and open the Actions panel. Add the following code in place of the TRace() statement currently within the code:

   var youWin:TextField = this.createTextField("youWin",¬      this.getNextHighestDepth(), 275, 250, 0, 0);    youWin.autoSize = "center";    youWin.text = "You Win";    youWin.background = true;    youWin.border = true;    youWin.selectable = false;    var formatter:TextFormat = new TextFormat();    formatter.size = 100;    formatter.color = 0xFF0000;    formatter.font = "_sans";    formatter.bold = true;    youWin.setTextFormat(formatter);


The preceding code adds a new text field that uses TextFormat to apply formatting to a You Win message.

3.

Test the movie.

When you win the game, a message will display.




Macromedia Flash 8 ActionScript Training from the Source
Macromedia Flash 8 ActionScript: Training from the Source
ISBN: 0321336194
EAN: 2147483647
Year: 2007
Pages: 221

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net