« Home « Kết quả tìm kiếm

Dynamically Creating and Configuring Text Fields


Tóm tắt Xem thử

- To create a text field using ActionScript, you use the createTextField() method, as follows:.
- Now note the following example, which uses the createTextField() method to create a text field:.
- 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.
- 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:.
- 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.
- myField_txt._alpha = 50;.
- makes the myField_txt text field 50 percent transparent.
- 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.
- 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..
- 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.
- In the following example, assume that the myField_txt text field contains the text, "I love my dog and cat very much!".
- 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!".
- 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.
- 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:.
- 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..
- This property represents the color of the border around a text field..
- myField_txt.borderColor = 0x336699;.
- myField_txt.border = true;.
- This read-only property represents the line number of the bottom- most visible line of text in a text field..
- If the hscroll property of a text field is set to anything greater than 0, all the text is shifted to the left.
- 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..
- imagine that a particular text field contains 14 lines of text, but can display only 7 lines at a time.
- myfield_txt.restrict = null;.
- myField_txt.restrict = "meatloaf";.
- myField_txt.restrict = "A-Z 0-9 a-z";.
- myField_txt.restrict .
- myField_txt.restrict = "^bug";.
- ActionScript can place any text in the field regardless of this property's setting..
- This property represents the number of the top line displayed in the text field.
- myField_txt.scroll = 4;.
- This read-only property determines the height of text in a field—not the height of the field itself, which can differ from the height of the text..
- This read-only property represents the width of text in a field—not the width of the field itself..
- This property determines whether the field is an input or dynamic text field..
- For example, this script makes myField_txt an input text field:.
- myField_txt.type = "input";.
- This property represents the name of the variable associated with the text field.
- As mentioned earlier, associating a variable with a text field causes that variable's value to be displayed in the field.
- When a text field is created, it has the following default property settings:.
- Background layer contains the scene's graphical content, with the exception of the buttons (named style1_btn, style2_btn, style3_btn, and style4_btn) that reside on the Buttons layer.
- The Components layer contains the applyToAll_cb Checkbox component that appears at the upper-left of the stage.
- The Box layer contains the movie clip instance named box_mc, just to the left of the stage.
- Other than some ActionScript, which will be placed on Frame 1 of the Actions layer, the white box is the only element in the scene used in this exercise.
- The smaller one is used to keep a running total of the number of characters (including spaces) in the larger field.
- Because transparent backgrounds behind text fields are not inherently possible, the white box (which is semitransparent) mimics the movement and size of the larger text field to act as its background..
- With the Actions panel open, select Frame 1 of the Actions layer and add the following script:.
- createTextField("movingField_txt .
- The first line of script creates a text field named movingField_txt, placed initially at x/y positions of 150 and 80, respectively, and with a width of 200 and a height of 20.
- Notice that movingField_txt has been set up as an input text field and can wrap lines of text.
- It's the larger of the two text fields that this project contains..
- Add the following script at the end of the current script:.
- The first line of script creates a text field named statusField_txt, placed initially at x/y positions of 150 and 80, respectively, and with a width of 100 and a height of 20.
- statusField_txt has been set up as a dynamic text field and cannot wrap lines of text.
- As the smaller of the two text fields, it mimics the larger field's movement.
- Add the following function definition at the end of the current script:.
- statusField_txt._x = movingField_txt._x;.
- statusField_txt._y = (movingField_txt._y + movingField_txt._height.
- statusField_txt.text = movingField_txt.length;.
- In a moment we'll script the movingField_txt to move as text is typed into it..
- statusField_txt at a specific relative distance from the movingField_txt text field as the latter field moves, and updates the text displayed in the statusField_txt text field..
- The function begins by setting the x position of the statusField_txt to match that of movingField_txt.
- The next line places the top of statusField_txt (its y position) 10 pixels below movingField_txt.
- It does this by adding the movingField_txt y.
- characters in movingField_txt.
- In a moment, we'll script this function to be called every time text is added to or removed from movingField_txt..
- _x = movingField_txt._x;.
- _y = movingField_txt._y;.
- _width = movingField_txt._width;.
- _height = movingField_txt._height;.
- When called, this function causes the box_mc movie clip instance to mimic the position and size of the movingField_txt text field, using familiar actions.
- As with the function defined in Step 4, we'll soon script this function to be called every time text is added to or removed from movingField_txt, making the box_mc movie clip instance act as the background for that text field..
- Add the following function calls at the end of the current script:.
- statusField_txt text field and the box_mc movie clip instance can be configured (as defined in the function definitions) as soon as the movie begins to play.
- movingField_txt.onChanged = function(){.
- movingField_txt._x.
- if (movingField_txt._x + movingField_txt._width >.
- movingField_txt._x = 150;.
- This section of script uses the onChanged event handler method (available to text field instances) to deal with what happens each time text is added to or removed from the movingField_txt text field..
- The function first moves movingField_txt four pixels from its current position, causing the text field to move to the right each time a character is added to or deleted from the field.
- It does this by comparing the right side of the field's position (achieved by adding the x position to the width of the field) with.
- If the right side of the field exceeds this amount, the field is set back to its starting x position of 150..
- statusField_txt text field and box_mc movie clip instance immediately each time text is added to or removed from the movingField_txt text field..
- The statusField_txt text field appears below the movingField_txt text field, and the box_mc movie clip instance has the same position and size as the movingField_txt text field.
- As text is typed into the movingField_txt text field, it moves to the right.
- statusField_txt follows the movement of movingField_txt, but displaying the current number of characters in the movingField_txt text field..
- After movingField_txt moves too far to the right, it's moved back to its original position.

Xem thử không khả dụng, vui lòng xem tại trang nguồn
hoặc xem Tóm tắt