Appending Text According to TextField Container Size

I have had a recent request to check the length of text in a TextField, and if it exceeds a certain size i.e. the width of its container, then to find the characters that precedes the container boundary and replace them with “…”

Here is the method I ended up using to achieve this:

1
2
3
4
5
6
7
private function checkLength( tf:TextField, container:Sprite ):void
{ 
  var firstChar:Number = tf.getCharIndexAtPoint( container.width-5, tf.y );
  var endChar:Number = tf.text.length;
              
  tf.replaceText( firstChar, endChar, "..." );
}

Comments