Flash CS5 Dynamic Text Formatting – Part 2

Mixing TextFormats within a TextField
We would like to highlight text within a paragraph. I would follow the instructions in Part 1 of this series, then follow these instructions to modify the text format within a paragraph:

  • create a new TextField with the desired font to be used
  • embed the font in the TextField
  • Set the font to be exported for actionscript*

* see the instructions at this site: AS3: Text Fields and Formats to set the linkage properties for the font in the IDE.

In my linkage Properties dialog, I set the class name of my desired font(and text format) to MsRefItalic.
In the code below, I access the new embedded font using this linkage class name. I then create a new TextFormat which will utilize the new font and also change a few of the text formatting attributes:

//create a reference to the font in the library which has
//been exported for actionscript with the class name 
//used to create the font
var italicFont:Font = new MsRefItalic();

//use this font to create an italic text format 
//for glossary terms
var italicTextFormat:TextFormat = new TextFormat();
italicTextFormat.font = italicFont.fontName;

//set the text italic
italicTextFormat.italic = true;

//make the text blue
italicTextFormat.color = 0x0756a3;

Now I want to modify some text within the paragraph and will start from the Part1 example:

//set movie clip text field value
choice_txt.text = "New Text to be displayed in the text field";
			
//apply text formatting to ALL new text to display
choice_txt.setTextFormat(choiceTextFormat);
			
//set the string to italicize and make blue
var italicBlueText:String = "displayed";

//hard coded above but could be dynamic, so
//make sure that there is a string
if (italicBlueText.length != 0){
	//get the string characters currently being
   //displayed in the text field
	var temp:String = choice_txt.text;
	
    //find the beginning index of word within string
	var beginIndex:int = temp.indexOf(italicBlueText);
						
	//set the end index based on the character lenght
	var endIndex:int=beginIndex + italicBlueText.length;
						
	//update the corresponding characters 
    //within the text field
	choice_txt.setTextFormat(this._italicTextFormat,beginIndex,endIndex);
}

Next in series, Part 3

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: