2010年11月12日 星期五

[AS3] setSelection()方法 - 動態選取文字欄位內的部份文字

selectable : Boolean -出文字欄位是否可選取的Boolean值,預設true
setSelection(beginIndex:int, endIndex:int):void
滑鼠點選爭間紅色背景文字欄位,觸發選取文字事件




package TextField
{
 //
 import flash.text.*;
 import flash.display.Sprite;
 import flash.events.MouseEvent;
 //
 public class setSelection_1 extends Sprite
 {
  //
  private var aTextField:TextField = new TextField();
  private var textContent:String = "No matter where you click on this text field the TEXT IN ALL CAPS is selected.";
  //
  public function setSelection_1()
  {
   //
   aTextField.text = textContent;
   //aTextField.autoSize = TextFieldAutoSize.LEFT;
   aTextField.width = 200;
   aTextField.height = 100;
   aTextField.x = 150;
   aTextField.y = 100;
   aTextField.wordWrap = true;
   aTextField.multiline = true;
   aTextField.border = true;
   aTextField.borderColor = 0xffff00;
   aTextField.background = true;
   aTextField.backgroundColor = 0xff0000;
   //文字欄位中文字的顏色,以十六進位格式表示。
   aTextField.textColor = 0x2222ff;
   //selectable : Boolean - 指出文字欄位是否可選取的Boolean 值。
   //selectable預設為true
   //aTextField.selectable = false;
   //
   addChild(aTextField);
   aTextField.addEventListener(MouseEvent.MOUSE_DOWN, selectText);
  }
  //
  private function selectText(e:MouseEvent):void
  {
   //setSelection(beginIndex:int, endIndex:int):void
   //beginIndex:int 和 endIndex:int 皆為0 base
   e.currentTarget.setSelection(49,65);
  }
 }
}


適用於 Adobe  Flash  Professional CS5 的 ActionScript  3.0 參考 - setSelection()方法

沒有留言: