In this lesson we will explain methods to create and add text in a Flash presentation using ActionScript 3.0, as well as a few properties and methods used to define text.
- Why create Text or other objects with ActionScript when they can be created directly on the stage? Because you can personalise the text added with ActionScript, and display the text you want dinamically, according on certain actions.
• To create text with AS3 you use the TextField class, this class contains many properties and methods to define text; such as, color, size, position and others.
Here are a few properties that can be applied directly to the instance for text.
// Initialize a "TextField" instance var txt:TextField = new TextField(); // set properties to define the text field txt.x = 50; // Distance from the left edge txt.y = 80; // Distance from the top edge txt.width = 200; // Lenght of the text field txt.textColor = 0x0000da; // Text color txt.backgroundColor = 0xededfe; // Sets the background color txt.background = true; // Enables to display the background colors txt.borderColor = 0x007800; // Sets the border color txt.border = true; // Enables to display the border txt.text = "Welcome to CoursesWeb.net \n Farewell"; // Adds text // Align the text in the created field txt.autoSize = "center"; // Apply "addChild()" to add the text in the Flash presentation addChild(txt);- "\n" adds a new line.
// Initialize a "TextField" instance var txt:TextField = new TextField(); // Defining properties for the text field txt.width = 180; txt.height = 100; txt.wordWrap = true; txt.multiline = true; // store CSS styles in a String variable var css_st:String = ".bv{color:#0808fe; font-family:Arial; font-size:16px; font-weight:bold;} .adf{color:#ed0708; font-family:Verdana; font-size:13px; font-style:italic}"; // Defines the instance for the "StyleSheet" object var styles:StyleSheet = new StyleSheet(); // Applies the "parseCSS" method to the variable with the CSS styles styles.parseCSS(css_st); // uses the "styleSheet" property to attach the CSS styles to the "txt" instance txt.styleSheet = styles; // Adds HTML formated text txt.htmlText = '<span class="bv">Welcome</span><br><font size="12" color="#00a800">to <u><a href="https://coursesweb.net">coursesweb.net</a></u></font><br><span class="adf">ActionScript course</span>.'; // add the text in the Flash presentation addChild(txt);- This code will show a Flash presentation like in this image:
The TextFormat object gives another way to format and define a text in a "textField" instance.
To use TextFormat, you must create an object with new TextFormat(). This object has specific properties and methods to format text, these are presented on this page: TextFormat properties and methods
The instance of the TextFormat must be added to the object which contains the text (created with "textField").
// Create a TextFormat instance var textF:TextFormat = new TextFormat(); // sets properties to format the text textF.leftMargin = 55; // the left margin of the paragraph, in pixels textF.font = "Arial"; // Defines the font used textF.color = 0x5678fe; // Color textF.size = 17; // Text size (in pixels) // Initialize a "TextField" instance in a "txt" variable var txt:TextField = new TextField(); // Defining properties for the text field txt.width = 240; txt.wordWrap = true; txt.multiline = true; txt.text = "Text with TextFormat \nActionScript 3 Course"; // Adds the Text // Apply "setTextFormat()" to the text field , with the "textFormat" instance as argument txt.setTextFormat(textF); // add the text in the Flash presentation addChild(txt);- This code will display a Flash presentation like in this image:
txtField.setTextFormat(textFormat, beginIndex, endIndex);- "txtField" is the instance created with "TextField"
Characters used for text in a TextField instance can be restricted with REGEX expresions, using the property restrict.
Exemple:
textField.restrict = "0-9"; // Allows only numbers
textField.restrict = "0-9 A-F"; // Allows only numbers and capital letters from A to F
textField.restrict = "0-9 A-F ^ a-z"; // Allows numbers and capital letters from A to F, without small letters
<ul> <li>http://coursesweb.net/html/</li> <li>http://coursesweb.net/css/</li> </ul>
.some_class { display: list-item; }
var obj = { "courses": ["php", "javascript", "ajax"] }; var jsonstr = JSON.stringify(obj); alert(jsonstr); // {"courses":["php","javascript","ajax"]}
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>'; $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); $elm = $dochtml->getElementById("dv1"); echo $elm->nodeValue; // CoursesWeb.net