タイトル募集中3.0

しがないITソルジャーの雑記。

TextInputを少し改良したい

TextInputの上にうっすらと文字を出しておいて,クリックとかされると消す,というのを作ってみた.

ほんとは,

public class CustomTextInput extends TextInput

でやってたんだけど,うまくフォーカスが移らなくてあきらめた.

package
{
	import fl.controls.TextInput;
	import flash.display.Sprite;
	import flash.text.*;
	import flash.events.*

	public class CustomTextInput extends Sprite
	{
		private var input:TextInput;
		private var txt:TextField;
		
		public function CustomTextInput(str:String = "form", size:Object = 11, col:Object = 0xDCDCDC, font:String = "Verdana") 
		{
			input = new TextInput();
			
			txt = new TextField();
			txt.autoSize = TextFieldAutoSize.LEFT;
			txt.background = true;
			txt.selectable = false;
			txt.text = str;
			txt.setTextFormat( cFormat(size, col, font) );
			
			txt.x = 3;
			txt.y = 2;
			
			addChild(input);
			addChild(txt);
			
			txt.addEventListener(MouseEvent.CLICK, cOnClickHandler);
			input.addEventListener(FocusEvent.FOCUS_IN, cFocusInHandler);
			input.addEventListener(FocusEvent.FOCUS_OUT, cFocusOutHandler);
		}
		
		private function cOnClickHandler(e:MouseEvent):void 
		{
			input.setFocus();
		}
		
		private function cFocusInHandler(e:FocusEvent):void 
		{
			if (txt.visible)
				txt.visible = false;
		}
		
		private function cFocusOutHandler(e:FocusEvent):void 
		{
			if (input.text == "") {
				txt.visible = true;
			}
		}
		
		private function cFormat(size:Object, col:Object, font:String):TextFormat
		{
			var format:TextFormat = new TextFormat();
            		format.color = col;
            		format.size = size;
            		format.font = font;
			return format;
		}
		
	}
	
}