TextLineMetricsの検証
TextLineMetricsの検証用FLASH(Matrixじゃないよ!)
フォントサイズを動的に変化させて、それによるプロパティの変化を
わかりやすいように数値を表示してみました。
無駄にドラッグとかもできます。
詳しいことはAdobeのヘルプでどうぞ
http://help.adobe.com/ja_JP/AS3LCR/Flash_10.0/flash/text/TextLineMetrics.html
package
{
/*
* TextLineMetricsの検証用FLASH(Matrixじゃないよ!)
* フォントサイズを動的に変化させて、それによるプロパティの変化を
* わかりやすいように数値を表示してみました。
*
* 無駄にドラッグとかもできます。
*
* 詳しいことはAdobeのヘルプでどうぞ
* http://help.adobe.com/ja_JP/AS3LCR/Flash_10.0/flash/text/TextLineMetrics.html
*/
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.FontStyle;
import flash.text.TextFieldAutoSize;
import flash.text.TextLineMetrics;
[SWF(width = "465", height = "465", frameRate = "30", backgroundColor = "#ffffff")]
public class Main extends Sprite
{
private var tfSp:Sprite = new Sprite();
private var tf:TextField = new TextField();
private var statusSp:Sprite = new Sprite();
private var statusTfArray:Array = new Array();
private var tf1:TextField = new TextField();
private var tf2:TextField = new TextField();
private var tf3:TextField = new TextField();
private var tf4:TextField = new TextField();
private var tf5:TextField = new TextField();
private var tf6:TextField = new TextField();
private var fontSizeTxt:TextField = new TextField();
private var _tFormat:TextFormat;
private var _posY:Number;
private var downBtn:Sprite = new Sprite();
private var upBtn:Sprite = new Sprite();
private var _fontSize:uint = 65;
private var _drag:Boolean = false;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
//StatusWindow関連
addChild(statusSp);
statusSp.mouseChildren = false;
statusSp.buttonMode = true;
statusSp.graphics.beginFill(0xFFEEEE);
statusSp.graphics.drawRect(0, 0, stage.stageWidth, 230);
statusSp.graphics.endFill();
statusSp.y = 220;
this.setChildIndex(statusSp,0);
statusSp.addEventListener(MouseEvent.MOUSE_DOWN, onDragStart);
statusSp.addEventListener(MouseEvent.MOUSE_UP, onDragStop);
_tFormat = new TextFormat(null, 20, 0x444444, FontStyle.BOLD);
_posY = 0;
statusTfArray = [tf1, tf2, tf3, tf4, tf5, tf6];
statusTfArray.forEach(TFInit);
//矢印ボタン
drawButton(upBtn);
upBtn.x = 400;
upBtn.name = "upBtn";
drawButton(downBtn);
downBtn.rotation = 180;
downBtn.x = 400;
downBtn.y = 200;
downBtn.name = "downBtn";
//フォントサイズ表示
fontSizeTxt.x = 10;
fontSizeTxt.y = 10;
fontSizeTxt.defaultTextFormat = new TextFormat(null, 20, 0x222222, FontStyle.BOLD);
fontSizeTxt.autoSize = TextFieldAutoSize.LEFT;
addChild(fontSizeTxt);
//メインのテキスト
tf.text = "Wonderfl world";
tf.width = stage.stageWidth;
tf.border = true;
tf.selectable = false;
tfSp.y = 50;
tfSp.addEventListener(MouseEvent.MOUSE_MOVE, onDrag);
tfSp.addEventListener(MouseEvent.MOUSE_DOWN, onDragStart);
tfSp.addEventListener(MouseEvent.MOUSE_UP, onDragStop);
tfSp.mouseChildren = false;
tfSp.addChild(tf);
addChild(tfSp);
changeMetrics();
}
private function TFInit(element:*,i:int,arr:Array):void
{
element.defaultTextFormat = _tFormat;
element.text = "test";
_posY += 30;
element.y = _posY;
element.autoSize = TextFieldAutoSize.LEFT;
statusSp.addChild(element);
}
private function drawButton(target:Sprite):void
{
target.graphics.beginFill(0x55AA55);
target.graphics.moveTo(0, 0);
target.graphics.lineTo(20, 37);
target.graphics.lineTo( -20, 37);
target.graphics.lineTo(0, 0);
target.graphics.endFill();
target.buttonMode = true;
target.addEventListener(MouseEvent.CLICK, onClick);
addChild(target);
}
private function onClick(e:MouseEvent):void
{
if (e.target.name == "upBtn") {
changeMetrics("up");
} else if (e.target.name == "downBtn") {
changeMetrics("down");
}
}
private function changeMetrics(arg1:String = ""):void
{
if (arg1 == "up") {
_fontSize++;
} else if (arg1 == "down") {
if (_fontSize > 1) _fontSize--;
}
tf.setTextFormat(new TextFormat(null, _fontSize, null, FontStyle.BOLD));
var metrics:TextLineMetrics = tf.getLineMetrics(0);
tf1.text = "metrics.ascent = " + metrics.ascent;
tf2.text = "metrics.descent = " + metrics.descent;
tf3.text = "metrics.height = " + metrics.height;
tf4.text = "metrics.leading = " + metrics.leading;
tf5.text = "metrics.width = " + metrics.width;
tf6.text = "metrics.x = " + metrics.x;
fontSizeTxt.text = "fontSize = " + _fontSize.toString();
}
private function onDragStart(e:MouseEvent):void
{
e.target.startDrag();
_drag = true;
}
private function onDrag(e:MouseEvent):void
{
if (_drag == true) {
changeMetrics();
}
}
private function onDragStop(e:MouseEvent):void
{
e.target.stopDrag();
_drag = false;
}
}
}