Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Actionscript _Text Class (See related posts)

Useful functions for adding dynamic text fields. Don't forget, you have to add the font of choice into the main movie Library for this to work (Library Panel Menu > New Font). The name you give the font is the string that you pass to the getTextFormat function for the "font" parameter. Setup your font styles with getTextFormat, setup your dynamic text box with getTextField, and then add text to the text box with appendTextField.

dynamic class _Text {
	static function getTextFormat(font:String,size:Number,color:Number,leading:Number,url:String,target:String){
		var fmt:TextFormat = new TextFormat();
		fmt.font = font;
		fmt.kerning = true;
		fmt.leading = (leading != undefined && leading != null) ? leading : 0;
		fmt.bold = false;
		fmt.size = (size != undefined && size != null) ? size : 11;
		fmt.color = (color != undefined && color != null) ? color : 0x000000;

		if (url != undefined && url != null) {
			fmt.url = url;
			fmt.target = (target != undefined && target != null) ? target : "_self";
		}
		return (fmt);
	}
	
	static function getTextField(targetMC,targetDepth,txtFieldName,x,y,w,h,txtObj){
		var targetDepth = (targetDepth != undefined && targetDepth != null) ? targetDepth : targetMC.getNextHighestDepth();
		var txtfld = targetMC.createTextField(txtFieldName, targetDepth, x, y, w, h);
		txtfld.antiAliasType = (txtObj.antiAliasType) ? txtObj.antiAliasType : "advanced";
		txtfld.sharpness = (txtObj.sharpness) ? txtObj.sharpness : -60;
		txtfld.thickness = (txtObj.thickness) ? txtObj.thickness : -100;
		txtfld.embedFonts = (txtObj.embedFonts) ? txtObj.embedFonts : true;
		txtfld.selectable = (txtObj.selectable) ? txtObj.selectable : false;
		txtfld.html = (txtObj.html) ? txtObj.html : true;
		txtfld.multiline = (txtObj.multiline) ? txtObj.multiline : true;
		txtfld.autoSize = (txtObj.autoSize) ? txtObj.autoSize : "left";
		
		if (txtObj.htmlText) txtfld.htmlText = txtObj.htmlText;
		if (txtObj.txtFormat) txtfld.setTextFormat(txtObj.txtFormat);
		
		if (txtObj.wordWrap == undefined || txtObj.wordWrap == null) txtfld._width = txtfld.textWidth + 10;
		else txtfld.wordWrap = txtObj.wordWrap;
		return txtfld;
	}
	
	static function appendTextField(txtfld,txtfrmt,txt){
		var beginIndex:Number = txtfld.htmlText.length;
		txtfld.setNewTextFormat(txtfrmt);
		txtfld.replaceText(beginIndex,beginIndex,txt);
	}
}

You need to create an account or log in to post comments to this site.


Click here to browse all 4860 code snippets

Related Posts