Text Field Resize Bug

by WLAD
Changing the textField's format in run time will result in an bug 

I need this functionality to work as im rendering something that is directly effected by the text field size.

I have to set the textFormat each time im rendering since the textFormat itself changes.

Any idea how to fix it ? 

*EDIT* 
solution : http://wonderfl.net/c/fcPL
♥0 | Line 80 | Modified 2015-10-03 22:51:51 | MIT License
play

ActionScript3 source code

/**
 * Copyright WLAD ( http://wonderfl.net/user/WLAD )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/25DDL
 */

package {
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.utils.setInterval;
    [SWF(width="465", height="465", backgroundColor="0x45C076", frameRate="60")]
    public class TextFieldResizeBug extends Sprite {
        public static var $log:TextField = new TextField;
        public function TextFieldResizeBug() {
            if ( stage ) init();
            else addEventListener(Event.ADDED_TO_STAGE, init );
            } private function init(e:Event = null):void {
            removeEventListener(Event.ADDED_TO_STAGE, init);graphics.beginFill(0x45C076);
            onInit(); $log.defaultTextFormat = new TextFormat('_sans', 16, 0xFFFFFF ); graphics.drawRect(0, 0, 10000, 10000);
            $log.autoSize = 'left'; addChild($log); graphics.endFill(); $log.selectable = false;
        }
        
        ////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////
        //                 READ INFO BUG INFO ON LINES 52 & 69 ;)
        ////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////
        
        private var tf:TextFormat = new TextFormat('_sans', 38, 0xFFFFFF, true);
        private var text:TextField;
        private var applyTextFormat:Boolean = true;
        
        private function onInit():void
        {
            text = new TextField();
            addChild( text );
            text.defaultTextFormat = tf;
            text.text = "Resize Bug";
            text.border = true;
            text.borderColor = 0;
                        text.multiline = true;
            text.autoSize = 'left';
            
            setInterval( function():void
            {
                if ( text.type == 'dynamic' ) 
                {
                    text.type = 'input';
                    text.mouseEnabled = true;
                    text.selectable = true;
                    text.type = "input";
                    stage.focus = text;
                    
                    if ( applyTextFormat ) {
                        
                        // Each time the formatting is applied the textField is resized for no reason
                        text.defaultTextFormat = tf;
                        text.setTextFormat( tf );
                        /////////////////////////////////////////////////////////////////////////////
                        
                    }
                }
                else 
                {
                    text.type = 'dynamic';
                    text.mouseEnabled = false;
                    text.selectable = false;
                    text.type = "dynamic";
                    stage.focus = null;
                    
                    if ( applyTextFormat ) {
                        
                        // Commenting out any line below still forces the bug to accure
                        text.defaultTextFormat = tf;
                        text.setTextFormat( tf );
                        ///////////////////////////////////////////////////////////////
                        
                    }
                }
                
                log( 
                    'text.width:<b>', text.width, 
                    '</b>text.textWidth:', text.textWidth,  
                    '\ntype<b>', text.type.toUpperCase(), 
                    "</b>\nApply text format set to: <b>", applyTextFormat.toString().toUpperCase(),
                    '</b>\n\n<font size="12">Click on the circle to toogle applyTextFormat</font>'
                );
                
                text.x = ( stage.stageWidth - text.width ) / 2;
                text.y = ( stage.stageHeight - text.height) / 2 - 100;
                
            }, 1000 );
            
            var circle:Sprite = new Sprite();
            circle.graphics.beginFill(0x0, 0.4);
            circle.graphics.drawCircle(stage.stageWidth / 2, stage.stageHeight * 0.5, 20);
            circle.graphics.endFill();
            circle.addEventListener('click', function(e:*):void { applyTextFormat = !applyTextFormat; } );
            circle.buttonMode = circle.useHandCursor = true;
            addChild( circle );
        }
        
        
        
    }
    
}

function log(...args):void
{
    TextFieldResizeBug.$log.htmlText = args.join('    ');
}

Forked