Macromedia Flash Professional 8 Unleashed
We've already gone over the interface, so you know how to make changes to text manually, but now we will go over how to change the formatting of the text with ActionScript. We will start out with the built-in object that Flash has for formatting text fields, called the TextFormat object. The TextFormat Object
Before you can start creating these TextFormat objects, you should know that when you create a text field with ActionScript, a default TextFormat object is applied to it with these values:
Notice that the textColor property in the TextField object does the same thing as the color property of the TextFormat object, so if all you want to do is change the color of the text, the TextFormat object is overkill; just use the textColor property of the TextField object. To create a new TextFormat object, you basically create a variable and set it to a new TextFormat object like this: var myFormat_fmt:TextFormat = new TextFormat(); Now we can start setting properties of this new TextFormat object like this: myFormat_fmt.align = "center"; myFormat_fmt.bold = true; myFormat_fmt.color = 0x00ff00;
We still have to apply the format we want to a text field before it can be rendered. There are two options for applying TextFormats to text fields. The first one is setTextFormat(), which has several different ways of being used:
It is also important to know that this method can only be applied after the text field has text in it. Also note that indexing strings start at 0, not 1. So the first character in any string has an index of 0. Let's go over a few examples of how to use this method. In the first example, we are going to put a small phrase in a text field, and then format that text field to look nice.
Now test the movie, and you should see your text formatted similar to Figure 15.7. Figure 15.7. Using the TextFormat object can produce great results.
In the next example, we are going to use the same method, but change it a bit, and use a loop to gradually change the characters to the format.
Now when you test the movie this time, it will gradually change over to the format instead of all at once as it did before. The final example of using the setTextFormat() method will set the text format to just one word.
This time when you test the movie, it will look like Figure 15.8. Figure 15.8. You can format just certain words in text using the setTextFormat() method.
As I mentioned earlier, there are two ways to set a TextFormat object to a text field, and the second way is using the setNewTextFormat() method, which is applied only one way, like this: textField.setNewTextFormat(textFormat);
The difference between this method and the preceding one is that this method will work now and forever on text fields. Basically that means that unlike the setTextFormat() method where it has to be used only after text has been placed inside a text field, any text put in after that will not only not have the text format, but will also clear the text format back to default values. But if you use the setNewTextFormat() method, all text from then on will use that format. Here is an example to help:
Now when you test it, you won't see anything terrificit will have just done what you had done before, but this time, you added text to the text field after you applied the format. That would have caused the loss of the format if you had used setTextFormat instead of setNewTextFormat. To see what it is meant by that, go back to the line that we set the format on, and change it: phrase_txt.setTextFormat(myFormat_fmt);
Now when you test, it looks as if the format was never applied, but in actuality it was applied and then it was cleared when you added text. Now you've seen how to format text inside text fields using the TextFormat object, but there are two more ways to format text. One is to use HTML (which we discuss later in this chapter) and the other way is to use Cascading Style Sheets. Cascading Style Sheets
Cascading Style Sheets (CSS) are not new to the Web, but since Flash can use them, it makes setting formats to text fields completely dynamic because you can actually create the style sheets in a text editor, save them as .css files, and load them in at runtime. Another great thing about their being dynamic is that you can have an HTML site and a Flash site all formatted with the same CSS. For those of you who are new to CSS, it was designed to help keep content and design separate in HTML. What that means is that you can create content, label the type of content it is (title, body, footnote, and so on), and then create a CSS to format those particular types of content and set the CSS to the content. That way, whenever you want to update content, you don't have to worry about it not being in the right format, and if you want to update the format, the content does not have to be touched. An example of this would be a newspaper. There is tons of content in a newspaper ranging from headlines, text body, writers' names, and even comments. But if there were no way of telling which was which, it would make the newspaper almost impossible to read with the title of the story, the writer's name, and the story itself all the exact same size and format. But the writers do not have time to set up format while they are writing the articles; they simply say this section is a headline, and that section is the body. Taking that concept over to cascading style sheets, the CSS is the formatter, and all it needs is definitions. So let's take a look at setting up some CSS inside ActionScript. The first thing you have to do is create a new StyleSheet object like this: var myStyle:TextField.StyleSheet = new TextField.StyleSheet(); Now that you have your StyleSheet, it's time to make some definitions for it using the setStyle() method like this: styleSheet.setStyle(styleName, theStyle);
In this method, the first parameter is the name of the specific style (which we will go into when we start implementing the style sheets) and the second is an object that has style properties set. There are two ways of implementing this. The first way is to create the style name and add attributes of that style all in the same method like this: myStyle.setStyle("title", {color: "#ff0000", fontSize: "20px"});
And the second way to implement the same method is to create a generic object, set the properties of the object the same way you would set style properties, and then pass the entire object to the style, like this: var tempStyle:Object = new Object(); tempStyle.color = "#ff0000"; tempStyle.fontSize = "20px"; //now pass the object to our already created StyleSheet myStyle.setStyle("title",tempStyle); //get rid of the temporary style object delete tempStyle;
Both of these implementations will work with our style sheet, so whichever you feel more comfortable using is what you should go with. Just remember, if you create temporary objects to hold your style properties, get rid of them when you are done to regain memory. Now that you've seen how to create the style sheet objects and how to set some styles to them, let's look at how to set them to our text, and how our text should be formatted to accept the styles. There are several different ways of setting text to the StyleSheet. The first one we will cover will use already supported tags such as <p> and <span>. You can use their built-in class attribute to set each class to an already specified style class like this. <p >This is<span > small </span></p>
And here is an example pulling the StyleSheet and the text together: //create the text field this.createTextField("phrase_txt",1,0,0,150,20); //set the html property to true phrase_txt.html = true; //create the StyleSheet object var myStyle:TextField.StyleSheet = new TextField.StyleSheet(); //create 2 style classes and set them to myStyle myStyle.setStyle(".title",{color: "#397dce", fontSize: "16px"}); myStyle.setStyle(".body",{color: "#000000", fontSize: "10px"}); //set the text field to the StyleSheet phrase_txt.styleSheet = myStyle; //set the text phrase_txt.htmlText="<p class='title'>This is<span class='body'> small </span> </p>"; A couple of things to notice in the preceding example; first, "styleSheet" beginning with a lowercase "s" is a property of the text field, and "StyleSheet" beginning with an uppercase "s" is an object class, so don't get those confused. And when we create style classes, we put a "." in front of the name and when we use them in our string, we don't need the period. Also, span tags are used inside the paragraph tag, and therefore must be closed before the paragraph tag. Here we used classes of styles, but we can also style an entire HTML tag so that anything residing in that tag will inherit the style we apply. Table 15.1 lists HTML tags in Flash that support having styles applied to them.
Here is an example using the a style name. //create the text field this.createTextField("link_txt",1,0,0,100,20); //set the html property to true link_txt.html = true; //create the StyleSheet object linkStyle = new TextField.StyleSheet(); //set the styles of the style sheet linkStyle.setStyle("a:link",{color: "#ff0000"}); linkStyle.setStyle("a:hover",{color: "#0000ff"}); linkStyle.setStyle("a:active",{color: "#00ff00"}); //set the StyleSheet to the text field link_txt.styleSheet = linkStyle; //set the text to the text field link_txt.htmlText = "<a href='http://www.sams.com' target='_blank'> Sams Publishing</a>";
Now you have seen how to format classes of tags as well as how to format the entire class. You can of course mix them together. For instance, you can define the entire <p> tag, and then define classes so that the main <p> tag acts more like a default setting. You can even create your own tags, and have them formatted to your own style names, as you will see in this next example. [View full width] //create the text field this.createTextField("news_txt",1,0,0,200,80); //set some of the properties news_txt.html = true; news_txt.wordWrap = true //create the StyleSheet object var myStyle:TextField.StyleSheet = new TextField.StyleSheet(); //define the styles myStyle.setStyle("title", {color: "#ff0000", fontSize: "20px"}); myStyle.setStyle("body",{color: "#000000", fontSize: "12px"}); //set the StyleSheet to the text field news_txt.styleSheet = myStyle; //now set the text news_txt.htmlText="<title>This just in</title><br><body> Flash supports CSS in text fields<
So far in CSS we have talked about creating the styles inside ActionScript, but that really isn't the point. The whole idea is to create the CSS files and store them outside of the Flash file to be loaded in. The method of StyleSheet used to load external files is the load() method, and when the CSS has been loaded, it triggers the onLoad event, which we will also be using in the following example.
We have covered a lot of different ways to format a text field (and we will cover one more in the HTML section later in this chapter). We have even covered how to keep content and design separate from one another using Cascading Style Sheets and loading them in from outside our Flash file. In this next section, we are going to cover how to manipulate the position using scroll properties, and we are going to cover the Mouse event that will make scrolling a lot easier. |
Категории