2010年11月18日 星期四

[AS3]TextField.getTextFormat(beginIndex, indIndex)方法

getTextFormat(beginIndex:int = -1, endIndex:int = -1):flash.text:TextFormat
會傳回 TextFormat 物件,包含 beginIndex 和 endIndex 參數所指定之文字範圍的格式資訊

UI組件Button的實體開啟了Toggle屬性,切換指定文字欄位tf3的TextFormat屬性



import fl.controls.Button;
import flash.events.Event;
import flash.text.TextFormat;
//
var tf1:TextField = new TextField();
tf1.autoSize = TextFieldAutoSize.LEFT;
tf1.text = "1.ABCDEFGHIJKLMNOPQRSTUVWXYZ";
tf1.x = 30;
tf1.y = 30;
tf1.background = true;
addChild(tf1);
//
var tf2:TextField = new TextField();
tf2.autoSize = TextFieldAutoSize.LEFT;
tf2.text = "2.ABCDEFGHIJKLMNOPQRSTUVWXYZ";
tf2.x = 50;
tf2.y = 60;
tf2.background = true;
addChild(tf2);
//
var tf3:TextField = new TextField();
tf3.autoSize = TextFieldAutoSize.LEFT;
tf3.text = "3.ABCDEFGHIJKLMNOPQRSTUVWXYZ";
tf3.x = 70;
tf3.y = 115;
tf3.background = true;
addChild(tf3);
//
var format1:TextFormat = new TextFormat();
format1.color = 0x0000FF;
format1.font = "04b_08";
format1.size = 24;
//
var format2:TextFormat = new TextFormat();
format2.color = 0xFF2255;
format2.font = "Times New Roman";
format2.size = 36;
//
var format3:TextFormat = new TextFormat();
format3.color = 0x662255;
format3.size = 42;
format3.font = "Orator Std";
//trace(format3);
//
var startRange:int = 7;//啟始字元 (0 base)
var endRange:int = 12;//結束字元+1 (0 base)
tf1.setTextFormat(format1);
tf2.setTextFormat(format2, startRange);
tf3.setTextFormat(format3, startRange, endRange);

//new運算子建構Button組件實體myButton
var myButton:Button = new Button();
addChild(myButton);
myButton.width = 250;
myButton.label = "取得 tf1 的 format1,據以改變 tf3 的文字格式";//取得或設定組件的文字標籤
myButton.toggle = true;//開啟-是否可以切換按鈕
/*selected屬性
http://help.adobe.com/zh_TW/Flash/CS5/AS3LR/fl/controls/LabelButton.html#selected
myButton.selected = true;
將Button組件實體myButton移動到座標(50,230)
*/
myButton.move(200, 210);
//;
var format:TextFormat = new TextFormat();
format = tf1.getTextFormat();
//
myButton.addEventListener(Event.CHANGE, changeFormat);
function changeFormat(e:Event):void
{
 if (e.currentTarget.selected)
 {
  tf3.setTextFormat(format, startRange, endRange);
  e.currentTarget.label = "回復 tf3 原本格式";
 }
 else
 {
  tf3.setTextFormat(format3, startRange, endRange);
  e.currentTarget.label = "取得 tf1 的 format1,據以改變 tf3 的文字格式";
 }

}


適用於 Adobe  Flash  Professional CS5 的 ActionScript  3.0 參考__flash.text.TextField.getTextFormat()方法

適用於 Adobe Flash Professional CS5 的 ActionScript 3.0 參考__fl.controls.LabelButton.selected屬性

沒有留言: