自定義文本編輯組件

自己寫的文本編輯組件, 可以完成文本水平垂直居中, 控制文字顯示區域(不超出組件), 設置文字格式(字體, 字號, 顏色, 粗體, 斜體, 下劃線), 並可輸入中文. 功能是都實現了, 但是代碼還有待提高, 如果有什麼更好的寫法, 請大家指點.

package textCompment
{
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.geom.Rectangle;
	import flash.system.IME;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	
	import mx.core.UIComponent;
	import mx.events.FlexEvent;
	
	public class TextElem extends UIComponent
	{
		/**水平佈局常量--左對齊*/
		public static const HORIZONTAL_LEFT:String = "left";
		/**水平佈局常量--居中對齊*/
		public static const HORIZONTAL_CENTER:String = "center";
		/**水平佈局常量--右對齊*/
		public static const HORIZONTAL_RIGHT:String = "right";
		/**垂直佈局常量--上對齊*/
		public static const VERTICAL_TOP:String = "top";
		/**垂直佈局常量--居中對齊*/
		public static const VERTICAL_MIDDLE:String = "middle";
		/**垂直佈局常量--下對齊*/
		public static const VERTICAL_BOTTOM:String = "bottom";
	
		/**文字內容*/
		private var _text:String;
		/**是否可編輯*/
		private var _editable:Boolean = true;
		/**當前水平佈局方式*/
		private var _currentHorizontalAlign:String;
		/**當前垂直佈局方式*/
		private var _currentVerticalAlign:String;
		
		/**當前組件中文本部分*/
		private var textField:TextField = new TextField();
		/**文本格式*/
		private var textFormat:TextFormat;
		/**顯示對象滾動矩形範圍*/
		private var rect:Rectangle = new Rectangle(0, 0);
		
		/**構造函數*/
		public function TextElem()
		{
			super();
			this.addEventListener(FlexEvent.CREATION_COMPLETE, init);
			textField.addEventListener(Event.CHANGE, textChangeHandler);
			this.addEventListener(FocusEvent.FOCUS_IN, textFocusInHandler);
		}
		
		/**
		 * 焦點移入監聽事件
		 * @parameter e FocusEvent
		 */
		private function textFocusInHandler(event:FocusEvent):void
		{
			IME.enabled = true;
		}
		
		/**
		 * 文本編輯監聽事件
		 * @parameter e Event
		 */
		private function textChangeHandler(event:Event):void
		{
			this._text = textField.text;
			if(this._currentVerticalAlign == TextElem.VERTICAL_MIDDLE)
			{
				this.textField.y = Math.round(this.height/2 - this.textField.height/2);
			} 
			else if(this._currentVerticalAlign == TextElem.VERTICAL_TOP)
			{
				this.textField.y = 0;
			}
			else if(this._currentVerticalAlign == TextElem.VERTICAL_BOTTOM)
			{
				this.textField.y = Math.round(this.height - this.textField.height);
			}
			
			if(textField.height > this.height)
			{
				this.textField.y = Math.round(this.height - this.textField.height);
			}
		}
		
		/**
		 * 初始化創建完成監聽事件
		 * @parameter e FlexEvent
		 */
		private function init(event:FlexEvent):void
		{
			textField.autoSize="left";
			textField.multiline = true;
			textField.wordWrap = true;
			textField.type = TextFieldType.INPUT;
			textField.y = Math.round(this.height/2 - this.textField.height/2);
			textField.width = this.width;
			
			textFormat = new TextFormat();
			textFormat.align = TextElem.HORIZONTAL_CENTER;
			textFormat.bold = false;
			textFormat.color = 0x000000;
			textFormat.italic = false;
			textFormat.size = 12;
			textFormat.underline = false;
			textField.defaultTextFormat = textFormat;
			
			rect.width = this.width + 2;
			rect.height = this.height + 2;
			this.scrollRect = rect;
			
			this.addChild(textField);
			
			this._currentHorizontalAlign = TextElem.HORIZONTAL_CENTER;
			this._currentVerticalAlign = TextElem.VERTICAL_MIDDLE;
			this.doubleClickEnabled = true;
		}
		
		/**
		 * 設置文本格式
		 * @parameter format 文本格式
		 * @parameter beginIndex 開始字符索引
		 * @parameter endIndex 結束字符索引
		 */
		public function setTextFormat(format:TextFormat, beginIndex:int=-1, endIndex:int=-1):void
		{
			this.textFormat = format;
			if(textField.length == 0)
			{
				textField.defaultTextFormat = textFormat;
			}
			else 
			{
				this.textField.setTextFormat(textFormat, beginIndex, endIndex);
			}
		}
		
		/**
		 * 獲取文本格式
		 * @parameter beginIndex 開始字符索引
		 * @parameter endIndex 結束字符索引
		 * @return 所選部分的文本格式
		 */
		public function getTextFormat(beginIndex:int=-1, endIndex:int=-1):TextFormat
		{
			var tf:TextFormat = textField.getTextFormat(beginIndex, endIndex);
			return tf;
		}
		
		/**更新佈局(組件拉伸的時候調用)*/
		public function updateLayout():void
		{
		}
		
		/**
		 * 設置組件大小
		 * @parameter width 組件寬度
		 * @parameter height 組件高度
		 */
		public function setSize(width:Number, height:Number):void
		{
			this.width = textField.width = width;
			this.height = height;
			
			rect.width = width + 2;
			rect.height = height + 2;
			this.scrollRect = rect;
			
			if(this._currentVerticalAlign == TextElem.VERTICAL_MIDDLE)
			{
				textField.y = Math.round((this.height / 2) - (textField.height / 2));
			}
			else if(this._currentVerticalAlign == TextElem.VERTICAL_BOTTOM)
			{
				textField.y = this.height - textField.height;
			}
			else if(this._currentVerticalAlign == TextElem.VERTICAL_TOP)
			{
				textField.y = 0;
			}
			
			if(textField.height > this.height)
			{
				this.textField.y = Math.round(this.height - this.textField.height);
			}
		}
		
		/**
		 * 設置文本編輯狀態
		 * @parameter value 布爾值, 表示文本是否可編輯, 可編輯爲true, 不可編輯爲false
		 */
		public function set isEdited(value:Boolean):void
		{
			if(value == true)
			{
				this.textField.type = TextFieldType.INPUT;
			}
			else
			{
				this.textField.type = TextFieldType.DYNAMIC;
			}
			this._editable = value;
		}
		
		/**
		 * 獲取文本編輯狀態
		 * @return 表示文本是否可編輯的布爾值, 可編輯爲true, 不可編輯爲false
		 */
		public function get isEdited():Boolean
		{
			return this._editable;
		}
		
		/**
		 * 設置文本內容
		 * @parameter value 文本中輸入的字符串
		 */
		public function set text(value:String):void
		{
			this._text = textField.text = value;
		}
		
		/**
		 * 設置文本內容
		 * @return 文本中輸入的字符串
		 */
		public function get text():String
		{
			return _text;
		}
		
		/**
		 * 設置文本水平佈局方式
		 * @parameter value 代表水平對齊方式的字符串
		 * TextElem.HORIZONTAL_LEFT 左對齊
		 * TextElem.HORIZONTAL_CENTER 居中對齊
		 * TextElem.HORIZONTAL_RIGHT 右對齊
		 */
		public function set horizontalAlign(value:String):void
		{
			this._currentHorizontalAlign = this.textFormat.align = value;
			if(textField.length == 0)
			{
				textField.defaultTextFormat = textFormat;
			}
			else 
			{
				this.textField.setTextFormat(textFormat);
			}
		}
		
		/**
		 * 獲取文本水平佈局方式
		 * @return 代表水平對齊方式的字符串
		 * TextElem.HORIZONTAL_LEFT 左對齊
		 * TextElem.HORIZONTAL_CENTER 居中對齊
		 * TextElem.HORIZONTAL_RIGHT 右對齊
		 */
		public function get horizontalAlign():String
		{
			return _currentHorizontalAlign;
		}
		
		/**
		 * 設置文本垂直佈局方式
		 * @parameter value 代表垂直對齊方式的字符串
		 * TextElem.VERTICAL_TOP 上對齊
		 * TextElem.VERTICAL_MIDDLE 居中對齊
		 * TextElem.VERTICAL_BOTTOM 下對齊
		 */
		public function set verticalAlign(value:String):void
		{
			this._currentVerticalAlign = value;
			if(value == TextElem.VERTICAL_MIDDLE)
			{
				textField.y = Math.round((this.height / 2) - (textField.height / 2));
			}
			else if(value == TextElem.VERTICAL_BOTTOM)
			{
				textField.y = this.height - textField.height;
			}
			else if(value == TextElem.VERTICAL_TOP)
			{
				textField.y = 0;
			}
		}
		
		/**
		 * 獲取文本垂直佈局方式
		 * @return 代表垂直對齊方式的字符串
		 * TextElem.VERTICAL_TOP 上對齊
		 * TextElem.VERTICAL_MIDDLE 居中對齊
		 * TextElem.VERTICAL_BOTTOM 下對齊
		 */
		public function get verticalAlign():String
		{
			return this._currentVerticalAlign;
		}
		
		/**
		 * 獲取文本選中部分開始索引值
		 * @return 文本選中部分開始索引值
		 */
		public function get selectionBeginIndex():int
		{
			return this.textField.selectionBeginIndex;
		}
		
		/**
		 * 獲取文本選中部分末尾索引值
		 * @return 文本選中部分末尾索引值
		 */
		public function get selectionEndIndex():int
		{
			return this.textField.selectionEndIndex;
		}
		
		/**
		 * 獲取文本字符串長度值
		 * @return 當前組件中輸入文本字符串長度值
		 */
		public function get length():int
		{
			return this.textField.length;
		}
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章