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;

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.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: