dcster2001
07-31-2009, 05:04 PM
I have a tried and true custom text field class called "InfoBox" that creates a textField along with formatting data to display text. It works great virtually everywhere I've used it, except in one instance, it fails to properly display dynamic text on the first call where I add a variable call to the string to be displayed. Interestingly enough, the same code works on subsequent calls, but just not the first!
I have had issues with authored text fields and authored components interfereing with the proper display of textFields in general, but this project has NO authored content, except for a few basic shape graphics. It does have programatically-generated components, however...
var numSubs:String = sector.numSubs.toString();
trace ("numSubs = " + numSubs);
numSubs_txt.setText("MAPPED SUBSECTORS: " + numSubs);
// Trace displays numSubs' value properly, but above line doesn't show numSubs first time through (displays "MAPPED SUBSECTORS:"), but does work on subsequent calls to the same code!
Here's the InfoBox code:
package src.shared.infoBox {
import flash.display.Sprite;
import flash.text.*;
public class InfoBox extends Sprite {
// Initialize the prior text history variables
private var priorTxt:String;
private var priorRefTxt:String;
// Initialize the text format variable
protected var format:TextFormat = new TextFormat();
// Initialize the text field variable
protected var txtfld:TextField;
public function InfoBox (w:int, h:int, multi:Boolean = false, type:String = "DYNAMIC", auto:String = "NONE") {
// Create textfield
createTxtfld(w, h, multi, type, auto);
// Set the text format;
setFormat();
}
// Create text field and add it to infoBox display list
protected function createTxtfld(w:int, h:int, multi:Boolean, type:String, auto:String) {
txtfld = new TextField();
txtfld.width = w;
txtfld.height = h;
txtfld.embedFonts = true;
txtfld.antiAliasType = AntiAliasType.ADVANCED;
txtfld.multiline = multi;
txtfld.selectable = false;
txtfld.wordWrap = multi;
txtfld.type = TextFieldType[type];
txtfld.autoSize = TextFieldAutoSize[auto];
addChild(txtfld);
}
// Set text format
public function setFormat (align:String = "left", color:uint = 0x00CC33, font:String = "Arial", size:uint = 10,
bold:Boolean = true, italic:Boolean = false, underline:Boolean = false, leading:int = 0):void {
var grid:String;
switch (align) {
case "center":
case "right":
grid = "SUBPIXEL";
break;
default:
grid = "PIXEL";
}
txtfld.gridFitType = GridFitType[grid];
format.align = align;
format.color = color;
format.font = font;
format.size = size;
format.bold = bold;
format.italic = italic;
format.underline = underline;
format.leading = leading;
format.leftMargin = 1;
format.rightMargin = 1;
}
// Assigns htmlText to text field
public function setHtmlText (txt:String):void {
txtfld.htmlText = txt;
}
// Retrieves htmlText from text field
public function getHtmlText ():String {
return txtfld.htmlText;
}
// Assigns text to text field
public function setText (txt:String):void {
txtfld.text = txt;
// Apply formatting
txtfld.setTextFormat(format);
}
// Retrieves text property from text field
public function getText ():String {
return txtfld.text;
}
// Assigns the current htmlText to the priorTxt history variable
public function saveHtmlText ():void {
priorTxt = txtfld.htmlText;
}
// Assigns the current text to the priorTxt history variable
public function saveText ():void {
priorTxt = txtfld.text;
}
// Assigns priorTxt to htmlText property of text field
public function resetHtmlText ():void {
setHtmlText(priorTxt);
}
// Assigns priorTxt to text property of text field
public function resetText ():void {
setText(priorTxt);
}
// Assigns the current htmlText to the priorRefTxt history variable
public function saveHtmlRefText ():void {
priorRefTxt = txtfld.htmlText;
}
// Assigns the current text to the priorRefTxt history variable
public function saveRefText ():void {
priorRefTxt = txtfld.text;
}
// Assigns priorRefTxt to htmlText property of text field
public function resetHtmlRefText ():void {
setHtmlText(priorRefTxt);
}
// Assigns priorRefTxt to text property of text field
public function resetRefText ():void {
setText(priorRefTxt);
}
public function getLineText(n:int):String {
return txtfld.getLineText(n);
}
// Resizes the text field
public function setSize (w:int, h:int):void {
txtfld.width = w;
txtfld.height = h;
}
}
}
I have had issues with authored text fields and authored components interfereing with the proper display of textFields in general, but this project has NO authored content, except for a few basic shape graphics. It does have programatically-generated components, however...
var numSubs:String = sector.numSubs.toString();
trace ("numSubs = " + numSubs);
numSubs_txt.setText("MAPPED SUBSECTORS: " + numSubs);
// Trace displays numSubs' value properly, but above line doesn't show numSubs first time through (displays "MAPPED SUBSECTORS:"), but does work on subsequent calls to the same code!
Here's the InfoBox code:
package src.shared.infoBox {
import flash.display.Sprite;
import flash.text.*;
public class InfoBox extends Sprite {
// Initialize the prior text history variables
private var priorTxt:String;
private var priorRefTxt:String;
// Initialize the text format variable
protected var format:TextFormat = new TextFormat();
// Initialize the text field variable
protected var txtfld:TextField;
public function InfoBox (w:int, h:int, multi:Boolean = false, type:String = "DYNAMIC", auto:String = "NONE") {
// Create textfield
createTxtfld(w, h, multi, type, auto);
// Set the text format;
setFormat();
}
// Create text field and add it to infoBox display list
protected function createTxtfld(w:int, h:int, multi:Boolean, type:String, auto:String) {
txtfld = new TextField();
txtfld.width = w;
txtfld.height = h;
txtfld.embedFonts = true;
txtfld.antiAliasType = AntiAliasType.ADVANCED;
txtfld.multiline = multi;
txtfld.selectable = false;
txtfld.wordWrap = multi;
txtfld.type = TextFieldType[type];
txtfld.autoSize = TextFieldAutoSize[auto];
addChild(txtfld);
}
// Set text format
public function setFormat (align:String = "left", color:uint = 0x00CC33, font:String = "Arial", size:uint = 10,
bold:Boolean = true, italic:Boolean = false, underline:Boolean = false, leading:int = 0):void {
var grid:String;
switch (align) {
case "center":
case "right":
grid = "SUBPIXEL";
break;
default:
grid = "PIXEL";
}
txtfld.gridFitType = GridFitType[grid];
format.align = align;
format.color = color;
format.font = font;
format.size = size;
format.bold = bold;
format.italic = italic;
format.underline = underline;
format.leading = leading;
format.leftMargin = 1;
format.rightMargin = 1;
}
// Assigns htmlText to text field
public function setHtmlText (txt:String):void {
txtfld.htmlText = txt;
}
// Retrieves htmlText from text field
public function getHtmlText ():String {
return txtfld.htmlText;
}
// Assigns text to text field
public function setText (txt:String):void {
txtfld.text = txt;
// Apply formatting
txtfld.setTextFormat(format);
}
// Retrieves text property from text field
public function getText ():String {
return txtfld.text;
}
// Assigns the current htmlText to the priorTxt history variable
public function saveHtmlText ():void {
priorTxt = txtfld.htmlText;
}
// Assigns the current text to the priorTxt history variable
public function saveText ():void {
priorTxt = txtfld.text;
}
// Assigns priorTxt to htmlText property of text field
public function resetHtmlText ():void {
setHtmlText(priorTxt);
}
// Assigns priorTxt to text property of text field
public function resetText ():void {
setText(priorTxt);
}
// Assigns the current htmlText to the priorRefTxt history variable
public function saveHtmlRefText ():void {
priorRefTxt = txtfld.htmlText;
}
// Assigns the current text to the priorRefTxt history variable
public function saveRefText ():void {
priorRefTxt = txtfld.text;
}
// Assigns priorRefTxt to htmlText property of text field
public function resetHtmlRefText ():void {
setHtmlText(priorRefTxt);
}
// Assigns priorRefTxt to text property of text field
public function resetRefText ():void {
setText(priorRefTxt);
}
public function getLineText(n:int):String {
return txtfld.getLineText(n);
}
// Resizes the text field
public function setSize (w:int, h:int):void {
txtfld.width = w;
txtfld.height = h;
}
}
}