Text Fields

Text Fields

Text fields show all types of tex in a project. Static text fields just display text, Dynamic text fields can change the content, for example, they can be link to a variable and shows it's value. An Input field can be used by the user to enter text.

If you are entering code in a class file and you want to use an input text field you will have to create a text field and set up it's type property to INPUT. See on the right the 3 types of text fields.

var input1 : textField = new TextField()
input1 . type = TextFieldType . INPUT;
addChild (input1);
myText . type=TextFieldType . INPUT
myText . type=TextFieldType . DYNAMIC
myText . type=TextFieldType . STATIC

 

Basic textField properties

You can define a number of display properties of a text Field:

var myText : textField = new TextField()
myText . x = 10;
myText . y = 10;
myText . width = 500
myText . height = 150
myText . border = true
MyText . borderColor=0x00FF00
myText . textColor=0x000000
myText . multiline= true/false
myText.wordwrap= true/false
myText.text= "Summer slideshow"
myText.selectable = false ;

addChild (myText);

We are also setting the x and y properties (lowecase!).
They set the horizontal and vertical coordinates of the upper-left corner of the text-field, counting from the (0,0)coordinate of the Stage.

We can set the width and height properties.
We can set the border and the borderColor.

The text property defines the text content of the textField!

If you are working in a class with a text field and you want to make sure that a cursor doesn't change to a text selection cursor when the player moves the mouse over the field, you should turn the selectable property of the field to false.

 

To define further formating settingst we need to use a TextFormat object, using an object contructor.
We can define inside a large number of properties (see below).
After that we will apply those settings to the textfield using the defultTextFormat property (to apply formating BEFORE you add the text content to the textfield) or the setTextFormat method (to add the formatting AFTER you add the text).

textFormat ( font: String=null , size: Object=null , color: Object=null , bold: Object=null , italic: String=null , underline: Object=null , url: String=null , target: Object=null , align: String=null ,
leftMargin:
Object=null , rightMargin: Object=null , indent: Object=null , leading: Object=null )




A TextFormat is a holder for a large number of properties, to use it you need to name the properties in the exact order, using null if you don't need to define some on your list. Once you have defined those you needed in order you can finish it. You can apply the same TextFormat object to all or a number of text fields in your project.

var myFormat : TextFormat = new TextFormat ("Arial", 24 , 0xFF00FF , true, false , null , null , null , "center") ;


n parameter type default value description
1 font String ="Times New Roman" Name of the font for the text written in quotation.
2 size Object = 12 an integer that indicates the size in pixels(points).
3 color Object = 0x000000 black A number containing 3 8 bit RGB components
4 bold Object = false Boolean value. false/true
5 italic Object = false Boolean value. false/true
6 underline Object = false Boolean value. false/true
7 url String = null URL to create a hyperlink.
8 target String = null Target window for the URL. If there is a url the default is " _self"
9 align String = "left" as textFormatAlign value. left, right, center in quotation.
10 leftMargin Object = 0 Of the paragraph in pixels.
11 rightMargin Object = 0 Of the paragraph in pixels.
12 indent Object = 0 Of the paragraph in pixels.
13 leading Object = 0  
 

var myText2 : textField = new TextField()

var inputFormating1 : TextFormat = new TextFormat ();
inputFormating1 . font = "Arial" ;
inputFormating1 . size = 12 ;
inputFormating1 . align = "center"

myText2 . defaultTextFormat = inputFormating1

myText 2. text= "This is a test of the inputFormating1 text"

addChild(myText2)

TextField Object constructor
Notice that the variable type is TextFormat (we are setting the TextFormat object).
If you only want to set a few properties that are not in sequence inside the TextFormat object, you could name them one by one.
we are seting the font, size and align properties.

Apply the TextFormat through the defaultText Format property