If you want to have different fonts, colors, or other styles on different lines within the same TextField, you have to do a little work, but it’s not too bad once it is set up. You use the TextFormat class to change any font style you want to the text you want. The formatting is defined only for the index range that you specify. You can also create URL links and change line leading, too.

There already has to be a TextField on the stage with the instance name “tf1” in order for this to work.
If you are dynamically creating a TextField, then this code has to be called from the textfield’s Event.ADDED_TO_STAGE handler, or equivalent, since the TextField is not completely accessible until after that point.
Plus, you will need to import the TextField class.

import flash.text.TextFormat;
// import flash.text.TextField;

// TextFormat params:
// font:String = null
// size:Object = null
// color:Object = null
// bold:Object = null
// italic:Object = null
// underline:Object = null
// url:String = null
// target:String = null
// align:String = null
// leftMargin:Object = null
// rightMargin:Object = null
// indent:Object = null
// leading:Object = null

// set up the text formatting:
var format1a_fmt:TextFormat = new TextFormat("Arial",10); // set font and size
var format2a_fmt:TextFormat = new TextFormat("Courier,20,0xff0000, null,null,null, null,null, null,null,null,null,-2); // set font, size, color, and the last '-2' changes the leading
var format3a_fmt:TextFormat = new TextFormat(null,14,0x0000ff); // change only the size and color, using the viewer's default system font

// set the text for the text area:
tf1.text = "This is just test title\nBut wait!\nthere's more!";

// set indices of desired text formatting:
var idx1a:int = 23; // limit for the format for the first line of text
var idx2a:int = idx1a + 10; // limit for the format for the second line of text
var idx3a:int = tf1.text.length; // limit for the format for the third line of text

trace("idx1a = " + idx1a);
trace("idx2a = " + idx2a);
trace("idx3a = " + idx3a);

// assign the text formatting:
tf1.textArea.setTextFormat(format1a_fmt, 0, idx1a);
tf1.textArea.setTextFormat(format2a_fmt, idx1a, idx2a);
tf1.textArea.setTextFormat(format3a_fmt, idx2a, idx3a);

– 30 –

Advertisements