Archive for the ‘Adobe Flash’ Category

h1

How to set styles on text input fields in ActionScript 3.0

January 23, 2010

Problem: Setting the text style on an input TextField doesn’t work as expected; for example,


...
var t:TextField = new TextField();
var f:TextFormat = new TextFormat();
...
t.type = TextFieldType.INPUT;
f.font = "Arial";
f.size = 20;
t.setTextFormat(f);
addChild(t);
...

Text typed into the text field appears in the default serif font, not in Arial 20.

Solution You can’t style user input or replaced text in Actionscript 3.0 with setTextFormat, instead, you have to use defaultTextFormat. Just replace the line that calls setTextFormat with


t.defaultTextFormat = f;

and the input field will use the text format in f correctly. This won’t work if you’re using an ActionScript 3.0 style sheet with the textfield, though.