Tips: Embed fonts and Input Textfield focus

Anoying thing with dynamic input textfields in AS3: when adding the field in your stage without text, there’s no way to have any focus on it.

_field.type = TextFieldType.INPUT;
_field.selectable = true;
_field.embedFonts = true;

var format:TextFormat = new TextFormat();
format.font = "Myriad Pro";
format.size = 12;
format.color = 0x000000;

_field.antiAliasType=AntiAliasType.ADVANCED;
_field.setTextFormat(format);
addChild(_field);

Result: Impossible to put text in it.
A part of the solution is to insert text before adding the field to the parent object.

_field.text="some text";
addChild(_field);

You got it, focus is there, but you also have this very unusefull text.
To resolve this problem, I used the Event.ADDED_TO_STAGE to clear text content.


_field.addEventListener(Event.ADDED_TO_STAGE, onAdd);

//then the listener handler

private function onAdd(evt:Event):void {
evt.currentTarget.text = "";
}

Here you have a clean input textfield, with an embed font, mouse and tab focus. Cool hu ?


About this entry