FlashTextEngineで縦書き文章を作成する

テキストエリアなどで、縦書きを選択することができないので、FlashTextEngineを使って画像上に縦書きで文字を書き込んでいく。

関数化して繰り返し使えるようにし、自由に指定した文章を表示切替できるようにした。

縦横のフラグを設定して、ローテーションや横書きマージンを設定して変更できるようにした。

 

import flash.display.*;
import flash.text.engine.TextLine;
import flash.text.engine.TextBlock;
import flash.text.engine.TextElement;
import flash.text.engine.ElementFormat;
import flash.text.engine.FontDescription;
import flash.text.engine.EastAsianJustifier;
import flash.text.engine.LineJustification;
import flash.text.engine.JustificationStyle;
import flash.text.engine.TextRotation;
import flash.display.Sprite;
 
 var sp:Sprite = new Sprite();
sp.graphics.beginFill(0xffffff,1);//背景を白色にする。
sp.graphics.drawRect(0,0,665,510);
target_mc.addChild(sp);//追加対象を指定して配置する。
 
//フォント書式
var fontDesc:FontDescription = new FontDescription();
fontDesc.fontName = "Kozuka Mincho Pro M";
 
//エレメントのフォーマット
var format:ElementFormat = new ElementFormat();
format.locale = "ja";//テキストのロケール。jaだと日本語。
format.fontSize=16;
format.fontDescription = fontDesc;//Fontを設定
 
//テキストエレメントを作る
var txtEle:TextElement = new TextElement(str , format);
 
//テキストブロック
var txtBlock:TextBlock = new TextBlock();
txtBlock.textJustifier = new EastAsianJustifier("ja",LineJustification.ALL_BUT_LAST, JustificationStyle.PUSH_OUT_ONLY);//行の配置方法
 
//縦書きにする。デフォルトは横向き
if(vflg == 1){
txtBlock.lineRotation = TextRotation.ROTATE_90;
}

 
txtBlock.content = txtEle;//テキストエレメントをコンテンツとして設定
 
//列幅
var txtW:uint;
if(vflg == 1){
txtW = sp.height;
}else{
txtW = sp.width;
}

 
var textLine:TextLine= txtBlock.createTextLine(null , txtW);
var posX:uint = sp.width;
var posY:uint = 0;
var cnt:uint = 0;
 
while(textLine != null){
sp.addChild(textLine);
cnt++;
 
//縦書き
if(vflg == 1){
posX -= (textLine.width + 3);//textLineの幅+マージン分、次座標を修正
textLine.x = posX;
textLine.y = posY;
}
 
//横書き
if(vflg == 0){
textLine.x = 0;
posY += format.fontSize + 3;
textLine.y = posY;
}
 
textLine.alpha = 1;
textLine = txtBlock.createTextLine(textLine , txtW);//次のTextLineオブジェクトを参照

}

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です