本次较上一篇自定义文本组件新增对文字的大小写修改, 实现全部大写, 首字母大写以及还原成改变大小写之前文本状态. 这次把测试Demo也放上来, 以作参考.
TextElem.as
package textCompment
{
public class TextElem extends UIComponent implements ITextElem
{
/**水平布局常量--左对齐*/
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);
//文本原件备份
private var previousText:String;
/**构造函数*/
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);
}
//校正文本大小写格式
if(this._currentUpperOrLower == "uppercaseAllLetters")
{
textField.text = textField.text.toLocaleUpperCase();
textField.restrict = "^a-z";
}
else if(this._currentUpperOrLower == "uppercaseFirstLetter")
{
if(textField.text.charAt(textField.text.length - 1) == " " || textField.text.length == 0)
{
textField.restrict = "^a-z";
}
else
{
textField.restrict = "^A-Z";
}
}
else if(this._currentUpperOrLower == "normal")
{
textField.restrict = null;
recordNormalText();
}
}
/**
* 初始化创建完成监听事件
* @parameter e FlexEvent
*/
private function init(event:FlexEvent):void
{
this.graphics.lineStyle(1, 0x00ff00, 1);
this.graphics.moveTo(0, 0);
this.graphics.lineTo(0, this.height);
this.graphics.lineTo(this.width, this.height);
this.graphics.lineTo(this.width, 0);
this.graphics.lineTo(0, 0);
textField.border = true;
textField.borderColor = 0xff0000;
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();