forked from: テキストエリアにあわせてフォントサイズを変更する

by Makoto_Tanaka forked from テキストエリアにあわせてフォントサイズを変更する (diff: 1)
♥0 | Line 31 | Modified 2010-02-19 21:05:29 | MIT License
play

ActionScript3 source code

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

<?xml version="1.0" encoding="utf-8"?>
<!-- forked from matsu's テキストエリアにあわせてフォントサイズを変更する -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" applicationComplete="appComplete();">
	<mx:Script>
		<![CDATA[
			[Bindable]
			private var _maxFontSize:uint = 20;
			private var _tempFontSize:uint = 0;
			
			private function appComplete():void
			{
				start();
			}
			public function start():void
			{
				_tempFontSize = _maxFontSize;
				var testStr:String = "あいうえおかきくけこさしすせそたちつてとなにぬねのあいうえおかきくけこさしすせそたちつてとなにぬねの";
				aaaText.text = bbbText.text = testStr;
			}
			private function bbbTextUpdateComplete(evt:Event):void
			{
				bbbText.setStyle("fontSize", _tempFontSize);
				// 微調整で、+4 する。おそらく Text の border の top, right, bottom, left が、それぞれ 1 px ずつあるのだと思う。
				if ( evt.currentTarget.textHeight + 4 > evt.currentTarget.height ) {
					_tempFontSize -= 1;
					bbbText.setStyle("fontSize", _tempFontSize);
				}
			}
		]]>
	</mx:Script>
	<mx:Text id="aaaText" width="200" height="60" fontSize="{_maxFontSize}"/>
	<mx:Text id="bbbText" width="200" height="60" updateComplete="bbbTextUpdateComplete(event);"/>
</mx:Application>