ADOBE FLEX 3
Developer Guide
367
Controlling sharpness, thickness, and anti-aliasing
By default, Flash Player or AIR determines the settings for text display controls like sharpness, thickness, and anti-
aliasing as text resizes, changes color, or is displayed on various backgrounds. In some cases, like when you have very
small or very large text, or text on a variety of unique backgrounds, you may want to maintain your own control over
these settings. You can override the Flash Playeror AIR settings using the
flash.text.TextRenderer class and its
associated classes, like the CSMSettings class. These classes give you precise control over the rendering quality of
embedded text. For more information about embedded fonts, see “Using embedded fonts” on page 366.
Note: The
flash.text.TextField.antiAliasType property must have the value AntiAliasType.ADVANCED in order
for you to set the sharpness, thickness, or the gridFitType property, or to use the
TextRenderer.setAdvancedAntiAliasingTable() method.
The following example applies custom continuous stroke modulation (CSM) properties and formatting to displayed
text using an embedded font called
myFont. When the user clicks on the displayed text, Flash Player applies the
custom settings:
var format:TextFormat = new TextFormat();
format.color = 0x336699;
format.size = 48;
format.font = "myFont";
var myText:TextField = new TextField();
myText.embedFonts = true;
myText.autoSize = TextFieldAutoSize.LEFT;
myText.antiAliasType = AntiAliasType.ADVANCED;
myText.defaultTextFormat = format;
myText.selectable = false;
myText.mouseEnabled = true;
myText.text = "Hello World";
addChild(myText);
myText.addEventListener(MouseEvent.CLICK, clickHandler);
function clickHandler(event:Event):void
{
var myAntiAliasSettings = new CSMSettings(48, 0.8, -0.8);
var myAliasTable:Array = new Array(myAntiAliasSettings);
TextRenderer.setAdvancedAntiAliasingTable("myFont", FontStyle.ITALIC,
TextColorType.DARK_COLOR, myAliasTable);
}
Working with static text
Static text is created only within the Flash authoring tool. You cannot programmatically instantiate static text using
ActionScript. Static text is useful if the text is very short and is not intended to change (as dynamic text can). Think
of static text as a sort of graphic element like a circle or square drawn on the Stage in the Flash authoring tool. While
static text is more limited than dynamic text, ActionScript 3.0 does support the ability to read the property values of
static text using the flash.text.StaticText class. In addition, you can use the flash.text.TextSnapshot class to read values
out of the static text.
ADOBE FLEX 3
Developer Guide
368
Accessing static text fields with the StaticText class
Typically, you use the flash.text.StaticText class in the Actions panel of the Flash authoring tool to interact with a
static text instance placed on the Stage. You may also work in ActionScript files that interact with a SWF file
containing static text. In either case, you cant instantiate a static text instance programmatically. Static text is created
in the Flash CS3 authoring tool.
To create a reference to an existing static text field in ActionScript 3.0, you can iterate over the items in the display
list and assign a variable. For example:
for (var i = 0; i < this.numChildren; i++) {
var displayitem:DisplayObject = this.getChildAt(i);
if (displayitem instanceof StaticText) {
trace("a static text field is item " + i + " on the display list");
var myFieldLabel:StaticText = StaticText(displayitem);
trace("and contains the text: " + myFieldLabel.text);
}
}
Once you have a reference to a static text field, you can use the properties of that field in ActionScript 3.0. The
following code is attached to a frame in the Timeline, and assumes a variable named
myFieldLabel assigned to a
static text reference. In the example, a dynamic text field named
myField is positioned relative to the x and y values
of
myFieldLabel and displays the value of myFieldLabel again.
var myField:TextField = new TextField();
addChild(myField);
myField.x = myFieldLabel.x;
myField.y = myFieldLabel.y + 20;
myField.autoSize = TextFieldAutoSize.LEFT;
myField.text = "and " + myFieldLabel.text
Using the TextSnapshot class
If you want to programmatically work with an existing static text instance, you can use the flash.text.TextSnapshot
class to work with the
textSnapshot property of a flash.display.DisplayObjectContainer. In other words, you create
a TextSnapshot instance from the
DisplayObjectContainer.textSnapshot property. You can then apply
methods to that instance to retrieve values or select parts of the static text.
For example, place a static text field that contains the text "TextSnapshot Example" on the Stage. Add the following
ActionScript to Frame 1 of the Timeline:
var mySnap:TextSnapshot = this.textSnapshot;
var count:Number = mySnap.charCount;
mySnap.setSelected(0, 4, true);
mySnap.setSelected(1, 2, false);
var myText:String = mySnap.getSelectedText(false);
trace(myText);
The TextSnapshot class is useful for getting the text out of static text fields in a loaded SWF, in case you want to use
the text as a value in another part of an application.

Get ADOBE® FLEX® 3: PROGRAMMING ACTIONSCRIPT™ 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.