posts - 38, comments - 2, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

扩展TextField,合理计算中文字符长度

Posted on 2009-07-03 14:47 AntiquMan 阅读(506) 评论(0)  编辑  收藏 所属分类: Flex
Code
 
01.//Copyright © 2009. Http://L4cd.Net All Rights Reserved.
02.package net.L4cd.display
03.{
04.    import flash.events.Event;
05.    import flash.events.TextEvent;
06.    import flash.text.TextField;
07.    import flash.utils.ByteArray;
08.  
09.    /**
10.     * 扩展TextField类,中文以2字符长度计算
11.     *
12.     * @author L4cd.Net
13.     * @playerversion Flash player 9
14.     * @langversion 3.0
15.     * @version 2009-06-16
16.     */
17.    public class TextFieldExt extends TextField
18.    {
19.        private var _maxChars:int = -1;
20.        public function TextFieldExt()
21.        {
22.            super();
23.              
24.        }
25.        override public function get maxChars():int
26.        {
27.            return _maxChars;
28.        }
29.        override public function set maxChars(value:int):void
30.        {
31.            _maxChars = value;
32.            if(maxChars<0)
33.            {
34.                removeEventListener(TextEvent.TEXT_INPUT,input);      
35.            }else
36.            {
37.                addEventListener(TextEvent.TEXT_INPUT,input);
38.                text = getTextByCharLength(text,maxChars);
39.            }
40.        }
41.        override public function get length():int
42.        {
43.            return getCharLength(text);
44.        }
45.        private function input(e:TextEvent):void
46.        {
47.            //拦截并阻止textinput事件,手动处理内容输入
48.            var textField:TextField = e.currentTarget as TextField;
49.            var temp:String = getTextByCharLength(e.text,maxChars - getCharLength(text) + getCharLength(selectedText));
50.            var index:int = selectionBeginIndex;
51.            replaceText(selectionBeginIndex,selectionEndIndex,temp);
52.            setSelection(index+temp.length,index+temp.length);
53.            dispatchEvent(new Event(Event.CHANGE,true));
54.            e.preventDefault();
55.        }
56.        /**
57.         * 获取字符长度,一个中文算2长度
58.         * @param txt
59.         * @return 返回长度值
60.         */    
61.        private function getCharLength(txt:String):int
62.        {
63.            var byte:ByteArray = new ByteArray();
64.            byte.writeMultiByte(txt,"gb2312");
65.            byte.position = 0;
66.            return byte.bytesAvailable;
67.        }
68.        /**
69.         * 截取指定长度的文本内容,一个中文算2长度
70.         * @param txt 需要截取的文本
71.         * @param length 需要截取的长度
72.         * @return 截取后的内容
73.         */    
74.        private function getTextByCharLength(txt:String,length:int):String
75.        {
76.            if(length<1)return "";
77.            var byte:ByteArray = new ByteArray();
78.            byte.writeMultiByte(txt,"gb2312");
79.            byte.position = 0;
80.            return byte.readMultiByte(Math.min(length,byte.bytesAvailable),"gb2312");
81.        }
82.    }
83.}




调用方法和普通TextField无异
1.import net.L4cd.display.TextFieldExt
2.var ext:TextFieldExt = new TextFieldExt();
3.ext.maxChars = 15;
4.addChild(ext);





一般的输入,粘贴等操作均没问题..
直接对text进行赋值没有进行限制 (原来的TextField也没限制)..
如需限制~可以调用一次maxChars = maxChars即可..

只有注册用户登录后才能发表评论。


网站导航: