Setting a custom text format on a TextInput control in Flash using ActionScript 3.0

by Peter deHaan on December 22, 2008

in TextInput

The following example shows how you can set a custom text format on a Flash ActionScript 3.0 TextInput component by setting the textFormat style to a TextFormat object.

Full code after the jump.

// ActionScript 3.0
/* Requires:
 * - TextInput component in Flash library
 */
import fl.controls.TextInput;
 
var textFmt:TextFormat = new TextFormat();
textFmt.color = 0xFF0000;
textFmt.font = "Courier New"
textFmt.size = 18;
 
var textInput:TextInput = new TextInput();
textInput.text = new Date().toString();
textInput.setStyle("textFormat", textFmt);
textInput.width = 300;
textInput.move(10, 10);
addChild(textInput);

{ 3 comments… read them below or add one }

1 Aaron 02.06.09 at 4:53 am

Can one vertically align text within a textinput field to the middle or the vertical center of the box? I have a textinput that I resized the height but the text stays in the upper left corner, which is pretty ugly. Any help you can give is very much appreciated.

2 Gabe E. 03.31.09 at 5:21 pm

Awesome :D! Just what I was looking for :)! Thanks for sharing, Peter :)!
This website is so useful :D! I love your posts!

3 Gabberwocky 07.06.09 at 3:34 pm

Ok, if I want to get my text formatted a certian way but I already have a bunch of different text boxes with different text on different keyframes, how do I copy the style of the text in one box and apply it to the next so I don’t have to keep changing the color on each text box. Is there a way to copy just the attributes of a text box?

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Previous post:

Next post: