Schemaphic Systems Blog

Sharing experiences in Flash, Flex and AIR

Truncate text in TextArea/TextField

with one comment

// truncates a string and adds a suffix for display [e.g. and so on …]

public static function truncate(_textField:TextField, suffix:String = “…”):void
  {
   var titleString:String = _textField.text;
   var i:int = titleString.length;
   var multiline:Boolean = _textField.multiline;
   var textHeight:Number = _textField.textHeight;
   
   if( multiline ){
    //TODO: for multiline textField
   } else {
    if( _textField.textWidth > _textField.width ){
     while( _textField.textWidth > _textField.width ){
      titleString = titleString.substring(0, –i);
      _textField.text = titleString;
     }
     //This line is to eleminate any partial words in the end of the label
     //titleString = titleString.substring( 0, titleString.lastIndexOf( ‘ ‘ ) );
     _textField.text = titleString + suffix;
    }
   }
  }

Advertisements

Written by Deepanjan Das

June 13, 2010 at 7:10 AM

One Response

Subscribe to comments with RSS.

  1. A more refined version – partially done.

    public function truncate(_textField:TextField, suffix:String = “…”):void
    {
    var titleString:String = _textField.text;
    var i:int = titleString.length;
    var multiline:Boolean = _textField.multiline;
    var textHeight:Number = _textField.textHeight;

    if( multiline ){
    //
    } else {
    if( _textField.textWidth > _textField.width ){
    while( _textField.textWidth > _textField.width ){
    titleString = titleString.substring(0, –i);
    _textField.text = titleString;
    }
    titleString = titleString.substring( 0, titleString.lastIndexOf( ‘ ‘ ) );
    _textField.text = titleString + suffix;
    }
    }
    }

    deepanjandas

    September 22, 2010 at 12:06 PM


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: