Join Joseph Labrecque for an in-depth discussion in this video TextField and TextFormat, part of Learning ActionScript.
In action script, Text fields are visual text containers which are used upon the stage. And Text Format objects contain the stylistic rules to apply to Text fields. In this lesson, we'll examine how to effectively apply a Text Format instance to a Text field. Which will be rendered upon our stage. So, the first thing we're going to do is import a variety of classes. First we'll import Flash.display.sprite so that we can extend it for this class.
And we'll import Flash.text.textfield as well as flash.text.textformat. Within our class we'll declare two new private variables. One being called My Text, which is a Text field, and the other being called My Format, which is a Text Format object. Within our constructuor function we are going to invoke Build Text Field. After that Text field is created, we're going to write some text within there invoking this custom function that we've created called Write Text. So, first let's look at the build Text field function. The first thing we're going to want to do is set up our Text Format object so that we can format the eventual Text Field approprately.
So we set My Format equal to a new Text Format. We set myformat.font equal to a font we're choose, in this case Ariel. We set myformat.size equal to 22 and myformat.color equal, in this case, to white. And there are a number of other formats that you can use here so,SOUND. We have Alignment, Block Indent, Bold, Bullet, Color and so forth.
A lot of different things to choose from but, for this example these will be just fine. The next thing that we do is create a new Text field. So My Text equals New Text field. And on the Text field we can set the background color. In this case, equal to black. The alpha, the width, and height, and in this case, we set the width equal to our stage, stage width. And the height equal to our stage, stage height.
Doing it this way, it's going to cover the entire stage, no matter if we even change the size of our application stage. We're going to set Wordwrap, Multiline, and Background equal to true. What these do is Wordwrap allows words to wrap, Multiline allows us to have a variety of different lines rendered in our Text field. And Background equals true is going to allow this background color to actually show up as a background of our Text field.
So then to apply our Text Format object, we say mytext.defaulttextformat=my format. So now any text that we place within my text is going to show up styled with my format. To add this to the stage we say this.addchild and then pass in our Text filled object, my text. So now that our Text field is done, we now need to write some text to that Text field.
And we're doing this with a little method here called Write Text, in which we pass in this string. So the Write Text method is very simple. It's down here. It's a little private function called Write Text, which accepts a string. And all we're doing is setting myText.text equal to whatever string is actually passed through here. So we set that equal to t. So, .text assigns all the text of that Text field to a certain string. And when we run this, it should render this text within the Text field and apply our proper formatting to it.
So, let's run it. All right. So, we can see here that it's created the Text field which is actually the size of our entire stage. And it's applied the styles that we wanted. So it's white text; it's arial, and it's also passed in that particular string and rendered that, within our Text field. So this has been an example on how to use both a Text field and Text Format objects.
With an action script.
- What is ActionScript and why should I learn it?
- ActionScript 2.0 vs. ActionScript 3.0
- Implementing external source code
- Working in Flash Professional and Flash Builder
- Understanding language fundamentals
- Working with ActionScript objects