Trimming a TextField with AS 3.0

I was recently tasked with building a utility class that was designed to trim text off a TextField object; it accepts 3 argument. The initial TextField, desired width, and text to be concatenated to the existing TextField.

So in short I had to truncate due to the width argument, but also add the concatenated text, which was usually "..." signifying the text had been truncated. The code is below. I hope it helps you if you come up against a similar challenge. Good luck!

package com.mapquest.tilemap.util
{
import flash.text.TextField;
import flash.text.TextFieldAutoSize;

public class TextFieldUtil
{

public function TextFieldUtil()
{}


/**
* Evaluates the textField and desired width param and sizes the TextField
*
* @returns sized TextField
* @param {number} desired width
* @param {TextField} text field to be edited
* @param {concatText} string to concat
*/
public function sizeTextField(textField:TextField, wid:Number, concatText:String):String
{
//size the text field and apply ellipses if it's text width is greater than desired width if (textField.textWidth > wid)
{

var initWidth:Number = textField.textWidth;
textField.appendText(concatText);
var concatWidth:Number = textField.textWidth - initWidth;
var activeWidth:Number = wid - concatWidth;

//change back to initial text textField.text = textField.text.substr(0, (textField.text.length - concatText.length))

//loop until size is right for (var i:int = 0; i < initWidth; i++)
{
if(textField.textWidth > activeWidth)
{

textField.text = textField.text.substr(0, (textField.text.length - 1));
}
else
{
textField.appendText(concatText);
break;
}
}
return textField.text;
}
else
{
return textField.text;
}
}
}
}

Comments
<A href="http://www.vipaocgold.com/" target=_blank><FONT size=4>aoc gold</FONT></A><FONT size=4>&nbsp; &nbsp;</FONT><A href="http://www.vipaocgold.com/buy-aoc-gold/" target=_blank><FONT size=4>buy aoc gold</FONT></A><FONT size=4>&nbsp;&nbsp;</FONT><A href="http://www.vipaocgold.com/aoc-news/" target=_blank><FONT size=4>cheap aoc gold</FONT></A><BR><A href="http://www.aocsale.com/" target=_blank><FONT size=4>age of conan gold</FONT></A><FONT size=4>&nbsp; &nbsp;</FONT><A href="http://www.aocsale.com/buy-aoc-gold/" target=_blank><FONT size=4>buy age of conan gold</FONT></A><FONT size=4>&nbsp; &nbsp;</FONT><A href="http://www.aocsale.com/aoc-news/" target=_blank><FONT size=4>cheap age of conan gold</FONT></A><BR><FONT size=4><A href="http://www.cheaperzone.com/" target=_blank><FONT size=4>warhammer gold</FONT></A>&nbsp; &nbsp;<A href="http://www.cheaperzone.com/Buy-WOW-Gold/WOW-Gold.Html" target=_blank>buy warhammer gold</A>&nbsp; &nbsp;<A href="http://www.cheaperzone.com/News/News.Html" target=_blank>cheap warhammer gold</A></FONT><BR><FONT size=4><A href="http://www.vipwarhammergold.com/" target=_blank><FONT size=4>warhammer gold</FONT></A>&nbsp; &nbsp;<A href="http://www.vipwarhammergold.com/" target=_blank>buy warhammer gold</A>&nbsp; &nbsp;<A href="http://www.vipwarhammergold.com/" target=_blank>cheap warhammer gold</A></FONT><BR><FONT size=4><A href="http://www.vipwargold.com/" target=_blank><FONT size=4>warhammer gold</FONT></A>&nbsp; &nbsp;<A href="http://www.vipwargold.com/" target=_blank>buy warhammer gold</A>&nbsp; &nbsp;<A href="http://www.vipwargold.com/" target=_blank>cheap warhammer gold</A></FONT><BR><FONT size=4><A href="http://www.buyfastgold.com/" target=_blank><FONT size=4>warhammer gold</FONT></A>&nbsp; &nbsp;<A href="http://www.buyfastgold.com/buy-warhammer-gold/" target="_blank">http://www.buyfastgold.com/buy-warhammer-gold/" target=_blank>buy warhammer gold</A>&nbsp; &nbsp;<A href="http://www.buyfastgold.com/news/" target="_blank">http://www.buyfastgold.com/news/" target=_blank>cheap warhammer gold</A></FONT><BR><FONT size=4><A href="http://warhammer-gold.rgtrcredit.com/" target=_blank><FONT size=4>warhammer gold</FONT></A>&nbsp; &nbsp;<A href="http://warhammer-gold.rgtrcredit.com/Buy-warhammer-gold.html" target=_blank>buy warhammer gold</A>&nbsp; &nbsp;<A href="http://warhammer-gold.rgtrcredit.com/Warhammer-Gold.html" target="_blank">http://warhammer-gold.rgtrcredit.com/Warhammer-Gol..." target=_blank>cheap warhammer gold</A></FONT><BR><FONT size=4><A href="http://warhammer.hellgate-pd.com/" target=_blank><FONT size=4>warhammer gold</FONT></A>&nbsp; &nbsp;<A href="http://warhammer.hellgate-pd.com/buy-warhammer-gold.html" target=_blank>buy warhammer gold</A>&nbsp; &nbsp;<A href="http://warhammer.hellgate-pd.com/cheap-warhammer-gold.html" target=_blank>cheap warhammer gold</A></FONT>
# Posted By warhammer gold22 | 9/4/08 8:48 AM

Copyright Strikefish, Inc., 2005. All rights reserved.