forked from: テキストのサイズを幅に合わせて変更する

by tora forked from テキストのサイズを幅に合わせて変更する (diff: 10)
♥0 | Line 32 | Modified 2012-11-23 11:15:31 | MIT License
play

ActionScript3 source code

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

// forked from sakusan393's テキストのサイズを幅に合わせて変更する
package 
{
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;
    import flash.utils.*;

    public class TextSizeFitTest extends Sprite 
    {
        public function TextSizeFitTest() 
        {
            this.graphics.beginFill(0x339933, 0.5);
            this.graphics.drawRect(0, 0, 400, 10);
            
            var t:TextField = new TextField();
            var tf:TextFormat = new TextFormat("_ゴシック", 40, 0x339933, true);
            t.defaultTextFormat = tf;
            t.text = "あいうえを";
            addChild(t);
            /*
            var timer:Timer = new Timer(200);
            timer.addEventListener(TimerEvent.TIMER, function():void { t.appendText("あ"); changeSize(t, tf); } );
            timer.start();
            */
            t.autoSize="left";
            var maxSize = 50;
            var w = 400;
            var h = 50;
            for (; maxSize-- && t.width > w || t.height > h; ) tf.size = maxSize, t.setTextFormat(tf);
            t.x=w-t.width>>1,t.y=h-t.height>>1;        
        }
        
        private function changeSize(t:TextField, tf:TextFormat, maxSize:int = 50,w:int = 400,h:int = 50):void
        {
            t.autoSize="left"
            for (; maxSize-- && t.width > w || t.height > h; ) tf.size = maxSize, t.setTextFormat(tf);
            t.x=w-t.width>>1,t.y=h-t.height>>1;    
        }
    }
}