Tải bản đầy đủ (.pdf) (13 trang)

Tài liệu Using TextFormat Objects ppt

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (41.1 KB, 13 trang )


< Day Day Up >

Using TextFormat Objects
Using HTML tags in strings of text (as in Lesson 12
, "Using XML with Flash") is not the
only way to style text dynamically: TextFormat objects provide all the formatting power
of HTML—and more!
As nothing more than special objects containing formatting properties for character and
paragraph styles, TextFormat objects offer functionality similar to that of Cascading Style
Sheets (CSS). After a TextFormat object has been defined, that object is then "applied" to
a text field or section of text in a field, causing the affected text to take on the formatting
style described by the object. A TextFormat object can be created and defined in one of
two ways. Look at the following syntax:

var myStyle:TextFormat = new TextFormat(nameOfFont, size, color, bold, italic);


In this syntax, a TextFormat object is created and its formatting settings configured
simultaneously. Here's an example:

var myStyle:TextFormat = new TextFormat("Arial", 12, 0x336699, true, true);


TIP
You can configure many additional formatting settings by using the constructor function.

The preceding syntax creates a TextFormat object named myStyle. This object represents
a format that uses the Arial font at a point size of 12, using the color blue, and in bold
italic. If you create the same TextFormat object using the alternative syntax, here's how it
would look:



var myStyle:TextFormat = new TextFormat();

with(myStyle){

font = "Arial";

size = 12;

color = 0x336699;

bold = true;

italic = true;

}


The preceding syntax uses a with statement to configure various formatting properties of
the newly created object. Either way works, but the latter syntax is generally easier to
read and use.
After you've created a TextFormat object, you can apply it to all or just a portion of the
current text in the field, or to any new text entered into the field.
To apply a TextFormat object to all the text in a text field, use the following syntax:

myField_txt.setTextFormat(myStyle);


This script causes all text in the myField_txt text field to be displayed in the formatting
style described by the myStyle TextFormat object. If the text had previously been styled

using a different TextFormat object, that style is overwritten with the newly applied style.
To apply a TextFormat object to a single character in a text field, use the following
syntax:

myField_txt.setTextFormat(27, myStyle);


This script causes the character at index position 27 to take on the style defined by the
myStyle TextFormat object.
To style a range of characters in a field, use the following syntax:

myField_txt.setTextFormat(27, 50, myStyle);


This script causes the characters between index positions 27 and 50 to take on the
formatting style described by the myStyle TextFormat object.

If you want the current text in the field to maintain its formatting while styling any new
text entered into the field, use the setNewTextFormat() method. Look at the following
syntax:

myField_txt.setNewTextFormat(myStyle);


Using this script, nothing in the myField_txt text field changes initially; however, any
new text entered into it (either by the user or via ActionScript) takes on the character and
paragraph formatting defined by the myStyle TextFormat object.
NOTE
This rule applies to new text entered at the end of any text currently in the field. If the
insertion point is moved somewhere in the middle of the text, that text takes on the same

formatting as the character just to the right of the insertion point.

At some point, you may need to know what formatting has been applied to a specific
character or an entire text field (that is, which TextFormat objects have been applied), so
that you can copy and apply that formatting elsewhere. You can get this information by
using the getTextFormat() and getNewTextFormat() methods. To understand how these
methods work, keep in mind that when you apply a TextFormat object to text in a field,
Flash keeps a record of it, so to speak. For example, consider the following script, which
assigns the myStyle TextFormat object to the myField_txt text field:

myField_txt.setTextFormat(myStyle);


Later, if you wanted another text field to be styled in the same way but weren't sure what
style had been applied, you could use the following script to find out:

var newStyle:TextFormat = myField_txt.getTextFormat();


This script creates a new TextFormat object named newStyle that's automatically defined
with the same formatting and character settings as the TextFormat object currently
applied to myField_txt. The newStyle TextFormat object can then be applied to text
fields. Just as the setTextFormat() method allows you to apply a format to a specific
character or range of characters (as opposed to an entire field), the getTextFormat()
method allows you to retrieve formatting data that has been applied at a specific character
index or to a range of characters. Look at the following example:

var newStyle:TextFormat = myField_txt.getTextFormat(27);



This script creates a new TextFormat object named newStyle that's automatically defined
with the same formatting and character settings as the TextFormat object currently
applied to the character at index position 27. If you want to retrieve the formatting that's
applied to new text entered into a field, as set with the setNewTextFormat() method, use
the following syntax:

var otherStyle:TextFormat = myField.getNewTextFormat();


This script creates a TextFormat object named otherStyle that's automatically defined
with the same formatting and character settings as the TextFormat object currently
applied to new text entered into the myField_txt text field.
TextFormat objects have numerous properties that can be set to describe the formatting
that the object represents. Many properties are self-explanatory, including align, bold,
color, leftMargin, rightMargin, and so on. You may not be as familiar with the following
properties:

font. This property represents the font face used by the object, using a string value
such as "Arial" or "Times New Roman". Using the getFontList() method of the
Text Field object in conjunction with this property, you can apply virtually any
font face the user currently has installed on his or her machine. (We'll demonstrate
the use of this property in the following exercise.)

tabStops. This property represents the distance (in points) that the caret moves
within a field when the Tab key is pressed. This property's value is set by
referencing the name of an array that contains positive numbers. For example, the
following script creates an array with five numbers:




var myTabStops:Array = [4, 20, 36, 52, 70];



To use the values in this array to set the tabStops property of the style1
TextFormat object, you would use the following syntax:

×