Using embedded fonts with the CheckBox control in Flash with ActionScript 3.0

by Peter deHaan on November 27, 2008

in CheckBox, Font

The following example shows how you can use an embedded font with the Flash ActionScript 3.0 CheckBox control by setting the textFormat and embedFonts styles.

Full code after the jump.

Note: The following example requires a CheckBox control in your Flash document’s library, as well as an embedded font with the linkage class set to “MyFont”.

// ActionScript 3.0
/* Requires:
 * - A CheckBox control in Flash library.
 * - An embedded font in Flash library with linkage class "MyFont".
 */
import fl.controls.CheckBox;
 
var embeddedFont:Font = new MyFont();
 
var textFormat:TextFormat = new TextFormat();
textFormat.font = embeddedFont.fontName;
textFormat.size = 24;
 
var checkBox:CheckBox = new CheckBox();
checkBox.setStyle("textFormat", textFormat);
checkBox.setStyle("embedFonts", true);
checkBox.label = "The quick brown fox jumps over the lazy dog.";
checkBox.textField.autoSize = TextFieldAutoSize.LEFT;
checkBox.move(10, 10);
checkBox.validateNow();
addChild(checkBox);

{ 6 comments… read them below or add one }

1 David 12.17.08 at 1:45 pm

Hello, that’s good if I want to change the font and size of the text of the checkbox, but if I want to use the checkbox to change the font and size of the text in a textarea, how I’m suppose the write the code?

2 Peter deHaan 12.17.08 at 2:17 pm

David,

Here’s some rough code (written in ActionScript 3.0).
To run this example, drag two CheckBox controls onto the Stage and give them instance names of ‘checkBox1′ and ‘checkBox2′. Next, drag a TextArea control onto the Stage and give it an instance name of ‘textArea’. Clicking the first CheckBox control should toggle the font family between “Times New Roman” and “Arial”. Clicking the second CheckBox control should toggle the font size between 12px and 20px.

// ActionScript 3.0
var textFmt:TextFormat = new TextFormat();
textFmt.font = "Arial";
textFmt.size = 12;
 
checkBox1.label = "Change font family";
checkBox1.width = textArea.width;
checkBox1.addEventListener(Event.CHANGE, checkBox1_change);
 
checkBox2.label = "Change font size";
checkBox2.width = textArea.width;
checkBox2.addEventListener(Event.CHANGE, checkBox2_change);
 
textArea.setStyle("textFormat", textFmt);
textArea.wordWrap = false;
textArea.text = describeType(TextArea).toXMLString();
 
function checkBox1_change(evt:Event):void {
    var tf:TextFormat = textArea.getStyle("textFormat") as TextFormat;
    if (evt.currentTarget.selected) {
        tf.font = "Times New Roman";
    } else {
        tf.font = "Arial";
    }
    textArea.setStyle("textFormat", tf);
}
 
function checkBox2_change(evt:Event):void {
    var tf:TextFormat = textArea.getStyle("textFormat") as TextFormat;
    if (evt.currentTarget.selected) {
        tf.size = 20;
    } else {
        tf.size = 12;
    }
    textArea.setStyle("textFormat", tf);
}

Peter

3 David 12.18.08 at 4:15 am

My god, you are quick like hell…
Seems so easy…when it’s write.
Thank you, I will try to understand every part.

4 vikram 12.26.08 at 11:00 pm

SENT ME THREE CHECK BOX FILE IN FLASH

5 vikram 12.26.08 at 11:06 pm

THANKS

6 rajesh 04.18.09 at 11:23 am

How we can change the font of check box in action script 2.0.
Please guide….

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: Using an embedded font with the TextArea control in Flash with ActionScript 3.0

Next post: Saving the contents of the Flash Actions panel to an external file using JSFL