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

Dynamically Creating and Configuring Text Fields

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 (39.19 KB, 15 trang )


< Day Day Up >

Dynamically Creating and Configuring Text Fields
Although you're presented with a variety of options when creating and configuring text
fields in the authoring environment, being able to create text fields on the fly—while
your movie is running—gives you even greater control over the way your project handles
text. Let's take a look at the dynamic creation process.
To create a text field using ActionScript, you use the createTextField() method, as
follows:

createTextField(instanceName, depth, x, y, width, height);


Now note the following example, which uses the createTextField() method to create a
text field:

createTextField("myField_txt", 10, 20, 50, 200, 30);


This script creates a text field named myField_txt, with its initial depth, x, y, width, and
height properties set as shown.
Every text field, whether placed on the stage while authoring the movie or created
dynamically, is considered an instance of the TextField class. As such, text fields can be
treated in several ways similarly to movie clip instances. Individual text fields are given
instance names from the Property Inspector (or when created dynamically, as shown in
the preceding code). When targeting a text field instance with a script, you use its target
path, which includes its instance name.
You may not be able to tell by looking at the Actions toolbox in the Actions panel, but
several methods—similar to Movie Clip object methods—are available to text field
instances. For example, this script makes the myField_txt text field draggable:



startDrag("myField_txt", true);


Unlike movie clips, text fields are not timelines; therefore, certain methods such as
prevFrame(), attachMovie(), loadMovie(), and so on have no meaning when used in the
context of a text field instance.
Text field instances have several properties similar to those of movie clips. For example,

myField_txt._alpha = 50;


makes the myField_txt text field 50 percent transparent. With a little bit of
experimentation, you'll be able to see which methods and properties are shared by movie
clip and text field instances.
In addition to the properties and methods discussed thus far, text fields have numerous
unique methods and properties for manipulating and controlling text within the text field
(rather than the text field itself, as previously discussed). Several of these methods are
employed to format text-field text using TextFormat and Cascading Style Sheet (CSS)
objects—we'll save our discussion of those for the later sections of this lesson. In the
meantime, let's look at a couple of useful methods that don't pertain to formatting.
To remove a text field instance that was created dynamically, use the removeTextField()
method in the following manner:

myField_txt.removeTextField();


This script removes the text field named myField_txt.
NOTE
Only text fields created dynamically can be removed using this method. Text fields

placed on the stage while authoring your movie cannot be removed in this way.

Two of the most common actions you can perform in conjunction with text fields are
adding and deleting text—both of which can use the replaceSel() method. You can
invoke this method to replace the currently selected text with anything you define.
In the following example, assume that the myField_txt text field contains the text, "I love
my dog and cat very much!" Then assume that the "dog and cat" portion of the text has
been selected. You can use the replaceSel() method, as shown in the following code, to
replace that text:

myField_txt.replaceSel("bird and snake");


The myField_txt text field now reads, "I love my bird and snake very much!"
The value placed within the replaceSel() method when it's invoked can be a string of text,
an empty string (""), or even a variable.

As mentioned earlier in this lesson, text fields have numerous properties that you can
configure using ActionScript while the movie is playing. These properties affect not only
the way the field looks and reacts to text, but how it functions. Some of these properties
are familiar because we've already used and discussed them; others don't warrant much
discussion because they do the same thing as the corresponding properties that you set
with the Property Inspector when creating a text field in the authoring environment. For
example, consider the selectable property. You would use the following syntax to make
the text in the myField_txt text field instance selectable:

myField_txt.selectable = true;


Now let's review some of the less obvious properties of text field instances:


autoSize. This property determines whether a text field expands and contracts its
borders automatically to accommodate the amount of text it contains. A text field's
width and height are normally set using static (unchanging) values. The AutoSize
property allows a text field's size to be dynamic—that is, determined by the text
that's typed into it.

borderColor. This property represents the color of the border around a text field.
You can set it using a hex value:



myField_txt.borderColor = 0x336699;



Setting the border color has no visible effect unless the border itself is visible:

myField_txt.border = true;


The same logic applies to the background and backgroundColor properties.

bottomScroll. This read-only property represents the line number of the bottom-
most visible line of text in a text field.


hscroll. This property is a numeric value representing in points the current
horizontal scrolling position. If the hscroll property of a text field is set to anything
greater than 0, all the text is shifted to the left. With a setting of 10, for example,

the text is left-shifted by 10 points. This property cannot be set to a value higher
than the maxhscroll property value for the same field and is only useful if word
wrapping is disabled in the text field, thus requiring horizontal scrolling to see text
that exists beyond the right boundary of the field.

maxhscroll. This read-only property represents the maximum amount that text can
be scrolled horizontally.


maxscroll. This read-only property represents the highest line number to which
text can be scrolled vertically in a field. To understand this property better,
imagine that a particular text field contains 14 lines of text, but can display only 7
lines at a time. Because you don't want the user to be able to scroll beyond line 14,
you would set the maxscroll property value to 8—a value that can change if lines
are added.


restrict. This property determines which characters can be entered into the field. A
value of null or "" indicates that any character can be entered into the field:



myfield_txt.restrict = null;



A specified string of text means that only characters included in that string can be
entered into the field:

myField_txt.restrict = "meatloaf";



A range of characters can be indicated using a hyphen:

myField_txt.restrict = "A-Z 0-9 a-z";


Using the caret symbol (^), you can specify a range of characters to accept, with

×