AS3 February 26th, 2008
TextFields are created in AS3 basically like most other objects, by first defining and then instantiating them:
Actionscript:
-
</code>
-
-
package {
-
import flash.display.Sprite;
-
import flash.text.*;
-
public class whynot extends Sprite{
-
private var display_txt:TextField;
-
-
public function whynot(){
-
display_txt = new TextField();
-
display_txt.text = "a text";
-
addChild(display_txt);
-
}
-
}
-
}
Here the code of a function I use for creating my textfields. still basic, but displays the most common settings needed for your dynamic textField. assumingyou have a font added in the library with the class instance name "_tahoma":
Actionscript:
-
private function makeTextField():void {
-
-
var tahoma:Font = new _tahoma();
-
-
TFFormat = new TextFormat();
-
-
TFFormat.font =tahoma.fontName;
-
TFFormat.color = "0xFFFFFF";
-
TFFormat.size = 11;
-
-
TextField = new TextField();
-
TextField.width = 200;
-
TextField.height = height;
-
TextField.x = 42;
-
TextField.y = 10;
-
TextField.wordWrap = true;
-
TextField.selectable = false;
-
TextField.border = true;
-
TextField.multiline = false;
-
TextField.embedFonts = true;
-
TextField.autoSize = TextFieldAutoSize.LEFT;
-
TextField.antiAliasType = AntiAliasType.ADVANCED;
-
-
addChild(alertTextField);
-
-
alertTextField.setTextFormat(alertTFFormat);
-
-
};
One issue I discovered that I can't really explain, is that I often have to reassign the textFormat to the TextField when changing the fields content:
Actionscript:
-
alertTextField.text = myString;
-
alertTextField.setTextFormat(alertTFFormat);
setting the focus on an input field:
http://livedocs.adobe.com/flex/201/langref/flash/display/Stage.html#focus
in AS3, it's now a property rather than a function that you call. Also, you don't use the text name value of the field, but a reference to the actual field itself...
would look something like:
Actionscript:
-
myObj.stage.focus = myTextField;
Leave a Reply
July 20th, 2008 at 10:50 pm
“One issue I discovered that I can’t really explain, is that I often have to reassign the textFormat to the TextField when changing the fields content”
use TextField.defaultTextformat = TFFormat
check my blog about this
http://greencollective.nl/blog/?p=16