Last Updated on February 26, 2019
The following examples show how you can get started with the TLFTextField class in ActionScript in Adobe Flash Professional CS5.
- The following example shows how you can create a simple TLFTextField instance using ActionScript:
- If you want to set word wrap on a TLFTextField instance, you can set the Boolean
wordWrap
property, as seen in the following example: - By default a TLFTextField instance is 100px by 100px. If you want to automatically resize the TLFTextField instance to the measured text width/height, you can set the
autoSize
property to one of the static constants from the flash.text.TextFieldAutoSize class, as seen in the following example: - If you want to set rich text formatting, you can set the
htmlText
instead of thetext
format, as seen in the following example: - If you want to set a custom text format to set the font color, face, size, etc., you can set the
defaultTextFormat
to a TextFormat object, as seen in the following example: - If you want to apply a custom text format to all characters or a range of characters in a TLFTextField, you can use the
setTextFormat()
method, as seen in the following example: - To set a custom text format to a range of characters, you can pass the optional start index and end index to the
setTextFormat()
method, as seen in the following example: - To set the text alpha on the TLFTextField instance you can set the
alpha
property, as seen in the following example: - To set the text rotation on the TLFTextField instance you can set the
rotation
property, as seen in the following example: - Similar to setting the
htmlText
property, setting thetlfMarkup
allows you to set rich text formatting on the TLFTextField instance, as seen in the following example: - Or you can load a TextFlow markup string dynamically at runtime using ActionScript, as seen in the following example:
import fl.text.TLFTextField;
var tlfTxt:TLFTextField = new TLFTextField();
tlfTxt.border = true;
tlfTxt.text = "The quick brown fox jumps over the lazy dog.";
tlfTxt.x = tlfTxt.y = 20;
addChild(tlfTxt);
import fl.text.TLFTextField;
var tlfTxt:TLFTextField = new TLFTextField();
tlfTxt.border = true;
tlfTxt.text = "The quick brown fox jumps over the lazy dog.";
tlfTxt.wordWrap = true;
tlfTxt.x = tlfTxt.y = 20;
addChild(tlfTxt);
import fl.text.TLFTextField;
import flash.text.TextFieldAutoSize;
var tlfTxt:TLFTextField = new TLFTextField();
tlfTxt.border = true;
tlfTxt.text = "The quick brown fox jumps over the lazy dog.";
tlfTxt.wordWrap = true;
tlfTxt.autoSize = TextFieldAutoSize.LEFT;
tlfTxt.x = tlfTxt.y = 20;
addChild(tlfTxt);
import fl.text.TLFTextField;
import flash.text.TextFieldAutoSize;
var tlfTxt:TLFTextField = new TLFTextField();
tlfTxt.border = true;
tlfTxt.htmlText = "The quick brown fox jumps over the lazy dog.";
tlfTxt.wordWrap = true;
tlfTxt.autoSize = TextFieldAutoSize.LEFT;
tlfTxt.x = tlfTxt.y = 20;
addChild(tlfTxt);
import fl.text.TLFTextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
var fmt:TextFormat = new TextFormat();
fmt.color = 0xFF0000; // red
fmt.font = "Arial";
fmt.size = 32;
var tlfTxt:TLFTextField = new TLFTextField();
tlfTxt.defaultTextFormat = fmt;
tlfTxt.border = true;
tlfTxt.text = "The quick brown fox jumps over the lazy dog.";
tlfTxt.wordWrap = true;
tlfTxt.width = 300;
tlfTxt.autoSize = TextFieldAutoSize.LEFT;
tlfTxt.x = tlfTxt.y = 40;
addChild(tlfTxt);
The defaultTextFormat
property must be set before the text
property.
import fl.text.TLFTextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
var fmt:TextFormat = new TextFormat();
fmt.color = 0xFF0000; // red
fmt.font = "Arial";
fmt.size = 32;
var tlfTxt:TLFTextField = new TLFTextField();
tlfTxt.border = true;
tlfTxt.text = "The quick brown fox jumps over the lazy dog.";
tlfTxt.setTextFormat(fmt);
tlfTxt.wordWrap = true;
tlfTxt.width = 300;
tlfTxt.autoSize = TextFieldAutoSize.LEFT;
tlfTxt.x = tlfTxt.y = 40;
addChild(tlfTxt);
The setTextFormat()
method must be called after setting the text
property.
import fl.text.TLFTextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
var defaultFmt:TextFormat = new TextFormat();
defaultFmt.font = "Arial";
defaultFmt.size = 32;
var redFmt:TextFormat = new TextFormat();
redFmt.color = 0xFF0000; // red
var tlfTxt:TLFTextField = new TLFTextField();
tlfTxt.border = true;
tlfTxt.defaultTextFormat = defaultFmt;
tlfTxt.text = "The quick brown fox jumps over the lazy dog.";
tlfTxt.setTextFormat(redFmt, 4, 9);
tlfTxt.wordWrap = true;
tlfTxt.width = 300;
tlfTxt.autoSize = TextFieldAutoSize.LEFT;
tlfTxt.x = tlfTxt.y = 40;
addChild(tlfTxt);
import fl.text.TLFTextField;
import flash.text.TextFieldAutoSize;
var tlfTxt:TLFTextField = new TLFTextField();
tlfTxt.border = true;
tlfTxt.text = "The quick brown fox jumps over the lazy dog.";
tlfTxt.wordWrap = true;
tlfTxt.autoSize = TextFieldAutoSize.LEFT;
tlfTxt.x = tlfTxt.y = 40;
tlfTxt.alpha = 0.3;
addChild(tlfTxt);
Unlike the TextField class which only supports setting text alpha on a TextField with an embedded font, the TLFTextField class supports setting text alpha on device (non-embedded) fonts.
import fl.text.TLFTextField;
import flash.text.TextFieldAutoSize;
var tlfTxt:TLFTextField = new TLFTextField();
tlfTxt.border = true;
tlfTxt.text = "The quick brown fox jumps over the lazy dog.";
tlfTxt.wordWrap = true;
tlfTxt.autoSize = TextFieldAutoSize.LEFT;
tlfTxt.x = tlfTxt.y = 40;
tlfTxt.rotation = 15;
addChild(tlfTxt);
Unlike the TextField class which only supports setting text rotation on a TextField with an embedded font, the TLFTextField class supports setting text retotation on device (non-embedded) fonts.
import fl.text.TLFTextField;
import flash.text.TextFieldAutoSize;
var tFlow:String = "The quick brown fox jumps over the lazy dog. ";
var tlfTxt:TLFTextField = new TLFTextField();
tlfTxt.border = true;
tlfTxt.tlfMarkup = tFlow;
tlfTxt.autoSize = TextFieldAutoSize.LEFT;
tlfTxt.x = tlfTxt.y = 40;
addChild(tlfTxt);
Or, if you want to apply some more complex formatting (such as hyperlinks, bold, italics, superscript/subscript, or strike through text, you can use a more complex TextFlow string object, as seen in the following example:
import fl.text.TLFTextField;
import flash.text.TextFieldAutoSize;
import flash.events.Event;
var tFlow:String = "The quick brown fox jumps over the lazy dog. ";
var tlfTxt:TLFTextField = new TLFTextField();
tlfTxt.border = true;
tlfTxt.tlfMarkup = tFlow;
tlfTxt.wordWrap = true;
tlfTxt.width = 300;
tlfTxt.autoSize = TextFieldAutoSize.LEFT;
tlfTxt.x = tlfTxt.y = 40;
addChild(tlfTxt);
import fl.text.TLFTextField;
import flash.display.Loader;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.text.TextFieldAutoSize;
var tlfTxt:TLFTextField = new TLFTextField();
tlfTxt.border = true;
tlfTxt.wordWrap = true;
tlfTxt.width = 300;
tlfTxt.autoSize = TextFieldAutoSize.LEFT;
tlfTxt.x = tlfTxt.y = 40;
addChild(tlfTxt);
var ldr:URLLoader = new URLLoader();
ldr.dataFormat = URLLoaderDataFormat.TEXT;
ldr.addEventListener(Event.COMPLETE, ldr_complete);
ldr.load(new URLRequest("markup.xml"));
function ldr_complete(evt:Event):void {
tlfTxt.tlfMarkup = ldr.data;
}
And the TLF markup file, markup.xml, is as follows:
The quick brown fox jumps over the lazy dog.
For more ActionScript examples, see ActionScriptExamples.com.
Chaitanya says
Dear team,
We are in the process of designing an automated flash file using Action Script 3.0.I would like to know the following details.
1. Can the .swf file be edited.If so what will be the completity of action script used in it is an animted movie.
2. Can i develop a GUI over the flash window.If yes how to go about it.
Freddy03h says
Hello,
I’ve Flash CS5 and I’ve got problem with TLFTextField : the autoSize doesn’t work. I try it on a TLFTextField already on the stage and on TLFTextFIeld create by code.
I can’t edit the autoSize of a TLFtextField by code using mytlftextfield.autoSize = TextFieldAutoSize.RIGHT;
Have you got the same problem ?
Thank’s and sorry for my english, i’m french.
Quarther says
Dear Peter,
This tutorial really helped me allot on starting with TLF text in FLASH CS5.
Thanks!
StingRay427 says
and how do i change SelectionColor on TLFTextField ?
friend says
anybody know how to close the window in projector with button?
Dubi says
Anybody knows how to use a bitmap text (no anti-alias at all), i can’t find that option , the same option that exists in the classic text field.
Suat says
Very nice and thanks. But i wonder if it is possible to place an object like MovieClip in library or load a swf from outside into TLFTextfield.
Berihun says
" rel="nofollow ugc">http://
Berihun says
I’m happy and very intersting to use this service.
Almog says
Any idea how to get this to work in Flash Builder 4 as a pure AS3 project having some issues with it
Peter deHaan (ActionScriptExamples.com) says
@Almog,
I believe that fl.text.TLFTextField is a Flash CS5 class only. If you’re using Flash Builder 4, you may want to use the TLF classes directly. For a simple example, see “Creating a simple TLF TextContainerManager in Flex 4” on FlexExamples.com.
Peter
bshankar says
Good tutorial. Very helpful.
mohammad adibi says
why text format doesn’t support negative leading?
Charla says
Thanks for this post! :)