Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

Flash MX action script bible - Reinhardt R.

Reinhardt R. Flash MX action script bible - Wiley & sons , 2004. - 987 p.
ISBN: 0-7645-4354-7
Download (direct link): macractionscriptbiblefeb2004.pdf
Previous << 1 .. 209 210 211 212 213 214 < 215 > 216 217 218 219 220 221 .. 427 >> Next

var tfFormatter:TextFormat = new TextFormat();
Once you’ve defined a TextFormat object in the preceding manner, you can define the values for its properties as discussed in subsequent sections.
The TextFormat constructor also enables you to define some of the object’s properties as you create the object. Frankly, this option is rarely used because it is difficult to remember the parameter order and because you must specify all 13 parameters to use it. However, if you find it useful, you can create a TextFormat object in the following way:
var tfFormatter:TextFormat = new TextFormat(font, size, color, bold, italic, underline, url, target, align, leftMargin, rightMargin, indent, leading);
If you prefer not to assign a specific value, you can use null. The following code creates a tfTitle object that uses BlurMedium font face at 10 point in blue, with a URL pointing to the
http://flashsupport.com Web site:
var tfTitle:TextFormat = new TextFormat("BlurMedium", 10,
0x0000FF, null, null, null, "http://flashsupport.com", "_blank", null, null, null, null, null);
As you can see, that syntax can be somewhat cumbersome. You might prefer the following syntax in order to accomplish the same thing:
var tfTitle:TextFormat = new TextFormat();
tfTitle.font = "BlurMedium";
tfTitle.size = 10;
tfTitle.color = 0x0000FF;
tfTitle.url = "http://flashsupport.com";
tfTitle.target = "_blank";
Also notice that you cannot specify block indentation and tab stops in the constructor. You must specify those values as properties. You can modify the properties of a TextFormat object subsequent to instantiating it regardless of which constructor you use.
Assigning Formatting to a TextField Object
Once you have created a TextFormat object, the next step is to apply that formatting to a TextField object’s contents. You can accomplish that with the setTextFormat() method of the TextField object to which you want to add the formatting. Using the setTextFormat() method you can add formatting to the entire contents, a single character, or a range of characters. Let’s take a look at these three options:
♦ setTextForma^TextFormaf object): This usage applies the properties of the specified TextFormat object to the entire contents of the TextField object from which the methods are called.
Chapter 18 ♦ Using the TextFormat Object and Style Sheets 443
♦ setTextFormat(rndex, TextFormat object): This syntax applies the TextFormat object properties to a specific character within the field. Each character is numerically indexed starting at 0. So to apply the formatting to the first character, you use the value of 0 for the first parameter.
♦ setTextFormat(beg*n/ndex, endlndex, TextFormat object): This syntax applies the TextFormat object properties to a specific range of characters within the field.
Now that you’ve had a chance to see the theory, let’s take a look at a few practical examples that use the setTextFormat() method in various ways.
First, the following code creates a TextField object, displays some text within it, and applies bold formatting to the entire text:
this.createTextField("tOutput", this.getNextHighestDepth(), 100, 100, 100, 20);
tOutput.text = "Formatted text";
var tfFormatter:TextFormat = new TextFormat();
tfFormatter.bold = true;
tOutput.setTextFormat(tfFormatter);
If you change the last line of code as follows, the bold formatting is applied only to the first letter:
t0utput.setTextFormat(0, tfFormatter);
And if you change the last line again to the following code, the bold formatting is applied only to the second word:
t0utput.setTextFormat(10, 14, tfFormatter);
The formatting that you apply to the TextField object is removed if and when you change the value of the object’s text property. That means you need to reapply the formatting when you change the text content programmatically. For example:
this.createTextField("tOutput", this.getNextHighestDepth(), 100, 100, 100, 20);
tOutput.text = "Formatted text";
var tfFormatter:TextFormat = new TextFormat();
tfFormatter.bold = true;
tOutput.setTextFormat(tfFormatter);
tOutput.text = "Different text";
tOutput.setTextFormat(tfFormatter);
It is also very important to note that changes you make to the TextFormat object are not applied to the TextField object until you reapply the TextFormat to the TextField. For example, the following code causes the text to appear bolded but not underlined:
this.createTextField("tOutput", this.getNextHighestDepth(), 100, 100, 100, 20);
tOutput.text = "Formatted text";
var tfFormatter:TextFormat = new TextFormat();
tfFormatter.bold = true;
tOutput.setTextFormat(tfFormatter);
tfFormatter.underline = true;
In order to get the underline setting to take effect, you need to call the setTextFormat() method again to reapply the formatting:
this.createTextField("tOutput", this.getNextHighestDepth(), 100, 100, 100, 20); tOutput.text = "Formatted text";
444 Part V ♦ The Movie Classes
var tfFormatter:TextFormat = new TextFormat(); tfFormatter.bold = true; tOutput.setTextFormat(tfFormatter); tfFormatter.underline = true; tOutput.setTextFormat(tfFormatter);
Previous << 1 .. 209 210 211 212 213 214 < 215 > 216 217 218 219 220 221 .. 427 >> Next