flash on 2012-4-27

by tepe
import flash.filters.*;
♥0 | Line 48 | Modified 2012-04-27 23:06:28 | MIT License
play

ActionScript3 source code

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

package {
    import flash.display.*;
    import flash.text.*;
    import flash.events.*;
    import flash.utils.*;
    //import flash.filters.*;

    public class TextAdjust extends Sprite {

        private var tf:TextField=new TextField();
        private var format:TextFormat;
        private var count:int = 1;

        public function TextAdjust() {

            // テキスト作製
            tf.wordWrap = true;
            tf.autoSize = TextFieldAutoSize.LEFT;
            tf.width = 240;
            tf.height = 100;
            
            tf.text = "kk ";
            //tf.htmlText = '<font color="#ff0000">'
            format = makeTextFormat(12, 0x000000);
            tf.setTextFormat(format);
            tf.type = "input";

            addChild(tf);
            setFontHeight(tf, format, 120);

            //タイマーの追加
           // var timer:Timer=new Timer(100,0);
           // timer.addEventListener(TimerEvent.TIMER,onTick);
            //timer.start();
            tf.addEventListener(Event.CHANGE,onChange);
        }

        private function onChange(e:Event):void {

            if(0<tf.length)setFontHeight(tf, format, 120);

        }

        /**
         * テキストフォーマットの生成
         * @param size サイズ
         * @param color 色
         */
        public static function makeTextFormat(size:uint,color:uint):TextFormat {
            var format:TextFormat=new TextFormat();
            format.size =size;
            format.color=color;
            format.bold =true;
            return format;
        }

        /** 
         * 一定高さ以下にフォントサイズを調整。height以下の高さに
         */
        public static function setFontHeight(label:TextField,format:TextFormat,height:uint):void {
            var i:uint;
            var lastSize:uint = 1;

            for(i = lastSize;; i++ ){
                format.size=i;
                label.setTextFormat(format);
                if (label.textHeight >= height){
                    break;
                }
                lastSize = i;
            }

            format.size = lastSize;
            label.setTextFormat(format);
        }


    }
}