自己写的文本编辑组件, 可以完成文本水平垂直居中, 控制文字显示区域(不超出组件), 设置文字格式(字体, 字号, 颜色, 粗体, 斜体, 下划线), 并可输入中文. 功能是都实现了, 但是代码还有待提高, 如果有什么更好的写法, 请大家指点.
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;
}
}
}