Truncate Text in ActionScript 3.0

Here’s a way to truncate the text inside of the text field in Actionscript 3.o.

// store the text field text
var $text:String = _textfield.text;

// marker
var $sign:String = "[@]";

// store the last line of visible text from the textfield
var $lastLine:String = _textfield.getLineText(_textfield.bottomScrollV - 1);

// split the last line into words
var $words:Array = $lastLine.split(" ");

// remove the last blank space
$words.pop();

// replace last word with dots
$words[$words.length-1] = "...";

// recreate the last line with the edited word
var $result:String = "";
for each(var $word:String in $words) $result += $word + " ";

// replace the last line of the text with a sign
$text = $text.split($lastLine).join($sign);

// cut the rest of the text
var $limit:int = $text.lastIndexOf($sign) + $sign.length;
$text =$text.slice(0, $limit);

// replace the sign with edited last line string
$text = $text.split($sign).join($result);

// add the new text to the text field
_textfield.text = $text;
Advertisements

3 thoughts on “Truncate Text in ActionScript 3.0

  1. Pingback: Truncate Text in ActionScript 3.0 « [ The Dark Realm ] « action script

  2. Pingback: Truncate Text in ActionScript 3.0 « [ The Dark Realm ] | Flash Designers

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