リプタクのもう覚えられません
Google
Web サイト内
topmemo ▶ テキストフォーマットで書式設定する

ActionScript

テキストフォーマットで書式設定する

TextFormatクラスで書式を設定するサンプル。

【サンプル】

JavaScriptを有効にし、最新のFlashPlayerをインストールお願いしますGet Adobe Flash player


【ActionScript3.0 サンプルソースコード】

//-------------------------------------------------------------------------------
//---------------------------------------------------- impprtステートメントの設定
//-------------------------------------------------------------------------------
import fl.data.DataProvider;
import fl.controls.ColorPicker;
import fl.events.ColorPickerEvent;
import fl.controls.Slider;
import fl.events.SliderEvent;

//-------------------------------------------------------------------------------
//------------------------------------------------------ テキストフィールドの設定
//-------------------------------------------------------------------------------
//テキストフィールド名
var tf1 = new TextField();

//テキストフィールド設定内容
tf1.x = 50;             // x 座標
tf1.y = 60;             // y 座標
tf1.width  = 450;       // 幅
tf1.height = 200;       // 高さ
tf1.wordWrap = true;    //折り返し
tf1.border = true;	    //枠線
stage.addChild(tf1);

//-------------------------------------------------------------------------------
//---------------------------------------------------- テキストフォーマットの設定
//-------------------------------------------------------------------------------
//テキストフォーマット名
var format1 = new TextFormat();

//テキストフォーマット設定内容(デフォルト)
format1.align = TextFormatAlign.LEFT;    // 段落の整列
format1.blockIndent = null;              // 全体のインデント(単位:ピクセル) 
format1.bold = null;                     // 太字
format1.bullet = null;                   // 箇条書き
format1.color = null;                    // 文字の色
format1.font = null;                     // フォント名
format1.indent = null;                   // 先頭文字のインデント
format1.italic = null;                   // 斜体
format1.kerning = null;                  // カーニング(埋め込みフォントに対応)
format1.leading = null;                  // 行間の垂直の行送り
format1.leftMargin = null;               // 段落の左マージン(単位:ピクセル)
format1.letterSpacing = null;            // 文字間スペースの量(単位:ピクセル)
format1.rightMargin = null;              // 段落の右マージン(単位:ピクセル)
format1.size = null;                     // 文字のポイントサイズ
format1.tabStops = null;                 // タブストップ
format1.target = null;                   // リンク先のターゲットウィンドウ
format1.underline = null;                // アンダーライン
format1.url = null;                      // リンク先 URL

// デフォルトの書式を設定
tf1.defaultTextFormat = format1;

//-------------------------------------------------------------------------------
//---------------------------------------------------------------------- テキスト
//-------------------------------------------------------------------------------
tf1.text = "テキストの\t書式をTextFormatクラスの 〜省略〜 メソッドを使用する。";

//-------------------------------------------------------------------------------
//------------------------------------------------------------------------ 1.font
//-------------------------------------------------------------------------------
var fontList:Array = [
{label:"Times"},{label:"Times New Roman"},{label:"_ゴシック"},{label:"_明朝"}
,{label:"_等幅"},{label:"_sans"},{label:"_serif"},{label:"_typewriter"},
];

combo1.dataProvider = new DataProvider(fontList);
combo1.selectedIndex = 0;
combo1.addEventListener(Event.CHANGE,fontTest);
function fontTest(e):void {

	switch (combo1.selectedLabel) {
		case "Times" :
			format1.font = "Times";               //Mac OS Xデフォルト
			tf1.setTextFormat(format1);
			break;
		case "Times New Roman" :
			format1.font = "Times New Roman";     //Windows デフォルト
			tf1.setTextFormat(format1);
			break;
		case "_ゴシック" :
			format1.font = "_ゴシック";
			tf1.setTextFormat(format1);
			break;
		case "_明朝" :
			format1.font = "_明朝";
			tf1.setTextFormat(format1);
			break;
		case "_等幅" :
			format1.font = "_等幅";
			tf1.setTextFormat(format1);
			break;
		case "_sans" :
			format1.font = "_sans";
			tf1.setTextFormat(format1);
			break;
		case "_serif" :
			format1.font = "_serif";
			tf1.setTextFormat(format1);
			break;
		case "_typewriter" :
			format1.font = "_typewriter";
			tf1.setTextFormat(format1);
			break;
	}

}

//-------------------------------------------------------------------------------
//----------------------------------------------------------------------- 2.color
//-------------------------------------------------------------------------------
cp2.addEventListener(ColorPickerEvent.CHANGE, fontcolor);
function fontcolor(event:ColorPickerEvent):void {
	format1.color = cp2.selectedColor; 
	tf1.setTextFormat(format1);
}

//-------------------------------------------------------------------------------
//------------------------------------------------------------------------ 3.size
//-------------------------------------------------------------------------------
sl3.addEventListener(SliderEvent.CHANGE,fontsize);
function fontsize(event:SliderEvent):void {
	format1.size = sl3.value; 
	tf1.setTextFormat(format1);
}

//-------------------------------------------------------------------------------
//----------------------------------------------------------------------- 4.align
//-------------------------------------------------------------------------------
var alignList:Array = [
{label:"LEFT"},{label:"RIGHT"},{label:"CENTER"},{label:"JUSTIFY"},
];

combo4.dataProvider = new DataProvider(alignList);
combo4.selectedIndex = 0;
combo4.addEventListener(Event.CHANGE,alignTest);
function alignTest(e):void {

	switch (combo4.selectedLabel) {
		case "LEFT" :
			format1.align = TextFormatAlign.LEFT;
			tf1.setTextFormat(format1);
			break;
		case "RIGHT" :
			format1.align = TextFormatAlign.RIGHT;
			tf1.setTextFormat(format1);
			break;
		case "CENTER" :
			format1.align = TextFormatAlign.CENTER;
			tf1.setTextFormat(format1);
			break;
		case "JUSTIFY" :
			format1.align = TextFormatAlign.JUSTIFY;
			tf1.setTextFormat(format1);
			break;
	}

}

//-------------------------------------------------------------------------------
//------------------------------------------------------------------------ 5.bold
//-------------------------------------------------------------------------------
cb5.addEventListener(MouseEvent.CLICK,boldTest);
function boldTest(e:MouseEvent):void {
	if (cb5.selected == true) {
			format1.bold = true;
			tf1.setTextFormat(format1);
	} else {
			format1.bold = false;
			tf1.setTextFormat(format1);
	}
}

//-------------------------------------------------------------------------------
//---------------------------------------------------------------------- 6.italic
//-------------------------------------------------------------------------------
cb6.addEventListener(MouseEvent.CLICK,italicTest);
function italicTest(e:MouseEvent):void {
	if (cb6.selected == true) {
			format1.italic = true;
			tf1.setTextFormat(format1);
	} else {
			format1.italic = false;
			tf1.setTextFormat(format1);
	}
}

//-------------------------------------------------------------------------------
//----------------------------------------------------------------- 7.blockIndent
//-------------------------------------------------------------------------------
sl7.addEventListener(SliderEvent.CHANGE,fontblockIndent);
function fontblockIndent(event:SliderEvent):void {
	format1.blockIndent = sl7.value; 
	tf1.setTextFormat(format1);
}

//-------------------------------------------------------------------------------
//---------------------------------------------------------------------- 8.indent
//-------------------------------------------------------------------------------
sl8.addEventListener(SliderEvent.CHANGE,fontindent);
function fontindent(event:SliderEvent):void {
	format1.indent = sl8.value; 
	tf1.setTextFormat(format1);
}

//-------------------------------------------------------------------------------
//------------------------------------------------------------------ 9.leftMargin
sl9.addEventListener(SliderEvent.CHANGE,fontleftMargin);
function fontleftMargin(event:SliderEvent):void {
	format1.leftMargin = sl9.value; 
	tf1.setTextFormat(format1);
}

//-------------------------------------------------------------------------------
//---------------------------------------------------------------- 10.rightMargin
//-------------------------------------------------------------------------------
sl10.addEventListener(SliderEvent.CHANGE,fontrightMargin);
function fontrightMargin(event:SliderEvent):void {
	format1.rightMargin = sl10.value; 
	tf1.setTextFormat(format1);
}

//-------------------------------------------------------------------------------
//-------------------------------------------------------------- 11.letterSpacing
//-------------------------------------------------------------------------------
sl11.addEventListener(SliderEvent.CHANGE,fontletterSpacing);
function fontletterSpacing(event:SliderEvent):void {
	format1.letterSpacing = sl11.value; 
	tf1.setTextFormat(format1);
}


//-------------------------------------------------------------------------------
//-------------------------------------------------------------------- 12.leading
//-------------------------------------------------------------------------------
sl12.addEventListener(SliderEvent.CHANGE,fontleading);
function fontleading(event:SliderEvent):void {
	format1.leading = sl12.value; 
	tf1.setTextFormat(format1);
}

//-------------------------------------------------------------------------------
//------------------------------------------------------------------ 13.underline
//-------------------------------------------------------------------------------
cb13.addEventListener(MouseEvent.CLICK,underlineTest);
function underlineTest(e:MouseEvent):void {
	if (cb13.selected == true) {
			format1.underline = true;
			tf1.setTextFormat(format1);
	} else {
			format1.underline = false;
			tf1.setTextFormat(format1);
	}
}

//-------------------------------------------------------------------------------
//------------------------------------------------------------------- 14.tabStops
//-------------------------------------------------------------------------------
sl14.addEventListener(SliderEvent.CHANGE,fonttabStops);
function fonttabStops(event:SliderEvent):void {
	format1.tabStops = [sl14.value]; 
	tf1.setTextFormat(format1);
}

//-------------------------------------------------------------------------------
//--------------------------------------------------------------------- 15.bullet
//-------------------------------------------------------------------------------
cb15.addEventListener(MouseEvent.CLICK,bulletTest);
function bulletTest(e:MouseEvent):void {
	if (cb15.selected == true) {
			format1.bullet = true;
			tf1.setTextFormat(format1);
	} else {
			format1.bullet = false;
			tf1.setTextFormat(format1);
	}
}

//-------------------------------------------------------------------------------
//-------------------------------------------------------------------- 16.kerning
//-------------------------------------------------------------------------------
cb16.addEventListener(MouseEvent.CLICK,kerningTest);
function kerningTest(e:MouseEvent):void {
	if (cb16.selected == true) {
			format1.kerning = true;
			tf1.setTextFormat(format1);
	} else {
			format1.kerning = false;
			tf1.setTextFormat(format1);
	}
}

//-------------------------------------------------------------------------------
//--------------------------------------------------------------------- 17.target
//-------------------------------------------------------------------------------
var targetList:Array = [
{label:"_self"},{label:"_blank"},{label:"_parent"},{label:"_top"},
];

combo17.dataProvider = new DataProvider(targetList);
combo17.selectedIndex = 0;
combo17.addEventListener(Event.CHANGE,targetTest);
function targetTest(e):void {

	switch (combo17.selectedLabel) {
		case "_self" :
			format1.target = "_self";
			tf1.setTextFormat(format1);
			break;
		case "_blank" :
			format1.target = "_blank";
			tf1.setTextFormat(format1);
			break;
		case "_parent" :
			format1.target = "_parent";
			tf1.setTextFormat(format1);
			break;
		case "_top" :
			format1.target = "_top";
			tf1.setTextFormat(format1);
			break;
	}

}

//-------------------------------------------------------------------------------
//--------------------------------------------------------------------- 18.url
//-------------------------------------------------------------------------------
cb18.addEventListener(MouseEvent.CLICK,urlTest);
function urlTest(e:MouseEvent):void {
	if (cb18.selected == true) {
			format1.url = "http://riptac.net";
			tf1.setTextFormat(format1);
	} else {
			format1.url = "";
			tf1.setTextFormat(format1);
	}
}
last update 2008.8.7
参考・出典等
Adobe Livedocs ActionScript 3.0 コンポーネントリファレンスガイド TextFormat

▲page top

topmemoblogtestaboutmail
Copylight(C)2008 Riptac. All Rights Reserved.