[ActionScript 3.0] 获取TextFiled字符边框

 1 /*****

 2  * getCharBoundaries () 方法  :返回一个矩形,该矩形是字符的边框。

 3  * getCharIndexAtPoint() 方法基于鼠标单击的 localX 和 localY 坐标来获取所单击的字符的索引,

 4    此坐标相对于包含它的 Sprite。如果该点(鼠标单击)不在任何字符上,getCharIndexAtPoint() 方法将返回 -1。

 5  * flash.text.TextField.caretIndex():int

 6    [只读] 插入点(尖号)位置的索引。如果没有显示任何插入点,则在将焦点恢复到字段时,值将为插入点所在的位置(通常为插入点上次  所在的位置,如果字段不曾具有焦点,则为 0)。 选择范围索引是从零开始的(例如,第一个位置为 0、第二个位置为 1,依此类推)。

 7  *

 8  * ****/

 9 

10 package 

11 {

12     import flash.display.Sprite;

13     import flash.events.FocusEvent;

14     import flash.events.MouseEvent;

15     import flash.text.TextField;

16     import flash.text.TextFormat;

17     import flash.geom.Rectangle;

18     import flash.events.MouseEvent;

19     import flash.text.TextFieldAutoSize;

20     import flash.display.Shape;

21 

22     public class TextField_getCharBoundariesExample extends Sprite

23     {

24         private var myTextField:TextField = new TextField  ;

25         private var myTextFormat:TextFormat = new TextFormat("微软雅黑",15);

26         private var spotlight:Shape = new Shape  ;

27 

28         public function TextField_getCharBoundariesExample()

29         {

30 

31             myTextField.x = 100;

32             myTextField.y = 100;

33             myTextField.border = true;

34             myTextField.autoSize = TextFieldAutoSize.LEFT;

35 

36             myTextField.text = "Selected a character from this text by clicking on it.浪断枫霜";

37             myTextField.setTextFormat(myTextFormat);

38             myTextField.addEventListener(MouseEvent.CLICK,clickHandler);

39             this.addChild(myTextField);

40             this.addChild(spotlight);

41         }

42 

43         private function clickHandler(e:MouseEvent):void

44         {

45             trace(e.localX,e.localY);

46             var index:int = myTextField.getCharIndexAtPoint(20,10);

47             //trace(index);

48             //trace(myTextField.caretIndex);

49             var frame:Rectangle = myTextField.getCharBoundaries(myTextField.caretIndex);

50 

51             spotlight.graphics.clear();

52             spotlight.graphics.beginFill(0xFF0000,.35);

53             spotlight.graphics.drawRect(frame.x + myTextField.x,frame.y + myTextField.y,frame.width,frame.height);

54             spotlight.graphics.endFill();

55         }

56     }

57 }

 

你可能感兴趣的:(actionscript)