Scrolling a dynamic textfield bugfix.

There has been a bug in flash that has always niggled me, and I’ve always wanted to try and find a solution but for one reason or another never had a time to. This bug occurs when you dynamically insert text in to a TextField in Flash which is allowed to autoSize and then select all the text by dragging with the cursor. In most cases, as you drag down the text will scroll up 1 line and you will loose the top line. This is a really ugly bug.

After reading Tomek’s blog post on the same bug and his fix , I’ve managed to discover what the problem is and the slightly modified (improved… cough, cough) on Tomek’s solution.

Tomek’s solution worked perfectly, which was to insert the text, store the height, set the autoSize property to NONE and then manually increase the height of the TextField. This means that the text fits within the resizes TextField fully and does not need to scroll.

The thing I didn’t like is that I was just adding 10 pixels to the height. What if I was using a large font size and that solution didn’t work? After some experimenting I found that the value you need to increase the height of the TextField by is it’s leading. So if your leading is 0 the bug will not appear, if you leading is much greater like 24, then you will need to add 24 pixels to the height of your dynamic TextField. It seems that when Flash resizes the TextField as you flow the copy in, it doesn’t take in to account the affect the leading will have on the overall height.

My solution is:

  1. Set the autoSize property to TextFieldAutoSize.LEFT.
  2. Insert the new text.
  3. Store the new height.
  4. Set the autoSize property to TextFieldAutoSize.NONE.
  5. Set the height to the stored height value and the leading value from the TextFields TextFormat object.
myTextField.autoSize = TextFieldAutoSize.LEFT;
myTextField.text = "lorem ipsum";
var h:Number = myTextField.height;
myTextField.autoSize = TextFieldAutoSize.NONE;
myTextField.height = h + myTextField.getTextFormat().leading;
This entry was posted in Technology and tagged , . Bookmark the permalink.

One Response to Scrolling a dynamic textfield bugfix.

  1. Mark Vice says:

    Great post!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">