TextField 小结

来源:互联网 发布:java潜艇大战 编辑:程序博客网 时间:2024/05/16 06:10

对于textfield中文本设置不同字体:

textfield可以对选中文本设置字体, 当一段文本有两种文本格式(即textformat)时, 可以获取光标所在位置前一个字符的textformat, 这样就可以取到不同样式文本的不同textformat, 而不影响其整体.但当修改整体文本格式时, 原有被修改的文本格式将丢失, 使用新定义的整体文本格式.

注:此处附带一小例子, 见本文最下方附件例一!

-------------------------------------------------------------------

对于textfield中文字设置两种颜色, 布局后变成一种颜色的修改:

new一个textformat赋值给当前textfield, textformat属性可以叠加, 而不要修改原有textformat的align属性.(同理可应用于其他属性)

-------------------------------------------------------------------

访问剪贴板:

System.setClipboard("文本内容");//将文本内容放入剪贴板Clipboard.generalClipboard.setData(ClipboardFormats.TEXT_FORMAT, "文本内容");//将文本内容放入剪贴板var t:String = Clipboard.generalClipboard.getData(ClipboardFormats.TEXT_FORMAT) as String;//读取剪贴板中内容
    注: 此处访问剪贴板需用户执行操作, 可以是按钮点击或键盘操作

-----------------------------------------------------------------
获取计算机安装字体:

var arr:Array = Font.enumerateFonts(true); arr.sortOn("fontName", Array.CASEINSENSITIVE);

------------------------------------------------------------------

TextField文本对齐:

1. 使用TextFormat的align属性

    textFormat.align = TextFormatAlign.CENTER;

    但是这个属性在myEclipse中给出的提示是指定的 align 不是 flash.text.TextFormatAlign 的成员, 用上面的方法没有效果. 

    所以就直接写 textFormat.align = "center"; 这样就好用了. 

2. 使用TextField的autoSize属性

    textField.autoSize = TextFieldAutoSize.RIGHT;

    但是这个属性在myEclipse中给出的提示是指定的 autoSize 不是 flash.text.TextFieldAutoSize 的成员, 用这个的方法也没有效果. 

    有人说是因为设置了TextField的wordWrap属性, 对autoSize产生了影响, 可是去掉了TextField属性, 也没有效果. (API没解释)

    注:textformat的align可实现两端对齐两端对齐, textfield的autoSize不能

---------------------------------------------------------------

TextField控制可编辑:

txttalk.type=TextFieldType.DYNAMIC;//不可编辑txttalk.type=TextFieldType.INPUT;//可编辑

--------------------------------------------------------------

TextField 设置可输入中文:

先获取焦点, 再修改IME:

input.addEventListener(FocusEvent.FOCUS_IN, focusInHandler);function focusInHandler(e:FocusEvent):void{    IME.enabled = true;}

--------------------------------------------------------------

textfield添加项目符号:

textformat.bullet = true;

---------------------------------------------------------------

textfield替换文本的方式:

textfield.text  ---> String

textfield.htmlText  ---> HTML格式文本

textfield.replaceSelectedText(String)  ---> 可用textfield.setSelection(beginIndex, endIndex)方法设置选中位置

textfield.replaceText(beginIndex, endIndex, String)

---------------------------------------------------------------

TextField 对文字编辑的监听:

这个事件可监听文字的增删改, 均可触发:

textField.addEventListener(Event.CHANGE, textChangeHandler);
网上好多都说用这个事件, 但是我加上之后, 这个事件只能监听文本添加, 对于删除不触发监听, 有待学习.
textField.addEventListener(TextEvent.TEXT_INPUT, textInputHandler);

  查看了TextArea的源码, 发现里面有个这个, 但是放到我的代码里没好用, 没找到原因为何, 在此先做记录.

 [Bindable("textChanged")]        // Compiler will strip leading and trailing whitespace from text string.    [CollapseWhiteSpace]           /**     *  @private     */    override public function set text(value:String):void    {        // Of 'text', 'textFlow', and 'content', the last one set wins.                super.text = value;                // Trigger bindings to textChanged.        dispatchEvent(new Event("textChanged"));            }


附件附件附件附件附件附件附件:

例一:

<?xml version="1.0" encoding="utf-8"?><s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"    xmlns:s="library://ns.adobe.com/flex/spark"    xmlns:mx="library://ns.adobe.com/flex/mx"><s:layout><s:VerticalLayout/></s:layout><fx:Script><![CDATA[import mx.controls.Alert;import mx.core.UIComponent;private var tf:TextField = new TextField();protected function button1_clickHandler(event:MouseEvent):void{tf.x = tf.y = 100;tf.width = 400;tf.height = 300;tf.border = true;tf.borderColor = 0x0000ff;tf.text = "好好学习 天天向上";tf.type = TextFieldType.INPUT;var tfor1:TextFormat = new TextFormat();tfor1.color = 0x00ffff;tfor1.size = 20;tfor1.font = "STCaiyun";//华文彩云tf.setTextFormat(tfor1, 0, 5);var tfor2:TextFormat = new TextFormat();tfor2.color = 0xff00ff;tfor2.size = 20;tfor2.font = "STXingkai";//华文行楷tf.setTextFormat(tfor2, 5, tf.length);tf.addEventListener(MouseEvent.CLICK, textClickHandler);var u:UIComponent = new UIComponent();u.addChild(tf);bc.addElement(u);}private function textClickHandler(e:MouseEvent):void{var t:TextFormat = null;if(tf.selectionBeginIndex == 0){t = tf.getTextFormat(tf.selectionBeginIndex, tf.selectionBeginIndex + 1);}else{t = tf.getTextFormat(tf.selectionBeginIndex - 1, tf.selectionBeginIndex);}Alert.show(t.font);}]]></fx:Script><fx:Declarations><!-- 将非可视元素(例如服务、值对象)放在此处 --></fx:Declarations><s:Button label="ClickMe" click="button1_clickHandler(event)"/><s:BorderContainer id="bc" width="100%" height="100%"/></s:WindowedApplication>