finder.as

来自「flex的一些小例子」· AS 代码 · 共 155 行

AS
155
字号
/*Copyright (c) 2007 FlexLib Contributors.  See:    http://code.google.com/p/flexlib/wiki/ProjectContributorsPermission is hereby granted, free of charge, to any person obtaining a copy ofthis software and associated documentation files (the "Software"), to deal inthe Software without restriction, including without limitation the rights touse, copy, modify, merge, publish, distribute, sublicense, and/or sell copiesof the Software, and to permit persons to whom the Software is furnished to doso, subject to the following conditions:The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.*/package flexlib.controls.textClasses{	import flash.text.TextField;		import flash.events.MouseEvent;		public class Finder	{		/**		* The TextField to be searched.		*/		private var textField:TextField;				/**		* The current starting index for searches. 		*/		private var caratIndex:int;				/**		* Finds strings in a TextField.		* 		* @param textField The TextField to search.		*/		public function Finder(textField:TextField){			this.caratIndex = 0;			this.textField = textField;			this.textField.addEventListener(MouseEvent.CLICK,setCarat,false,1,true);		}				/**		* Synchronizes the Finder's internal carat position with the TextField's carat position when the user manually sets it by clicking in the TextField.		*/		public function setCarat(evt:MouseEvent):void{            this.caratIndex = this.textField.caretIndex;        }        		/**		* Gets all indexes of a string in the TextField.		* 		* @param string The string to search for.		* @return An array of all the indexes of the string. 		*/        public function indexesOf(string:String, caseSensitive:Boolean=true):Array{            var pos:int = 0;            var r:Array = [];                        var txt:String = this.textField.text;            var len:int = string.length;                        if(!caseSensitive) {            	txt = txt.toLowerCase();            	string = string.toLowerCase();            }                        do{                if ((pos = txt.indexOf(string, pos)) != -1){                    r.push(pos);                }else{                    break;                }             }while(pos += len);            return r;        }                /**        * Finds the first instance of a string after the Finder's current carat position.        *         * @param string The string to search for.        * @return The character index of the string.        */        public function findNext(string:String, caseSensitive:Boolean=true):int{        	var str:String = this.textField.text;                        if(!caseSensitive) {            	str = str.toLowerCase();            	string = string.toLowerCase();            }                        var len:int = string.length;            var i:int = str.indexOf(string,this.caratIndex+len);                        if(i == -1){                this.caratIndex = 0;                i= str.indexOf(string,this.caratIndex);            }                        if(i == -1){                return -1;            }                        this.caratIndex = i+1;                        return i;                    }                /**        * Finds the first instance of a string before the Finder's current carat position.        *         * @param string The string to search for.        * @return The character index of the string.        */        public function findPrevious(string:String, caseSensitive:Boolean=true):int{       		             if(this.caratIndex == 0){                this.caratIndex = this.textField.text.length;            }            var txt:String = this.textField.text;            var str:String = txt.substring(0,this.caratIndex);                        if(!caseSensitive) {            	txt = txt.toLowerCase();            	str = str.toLowerCase();            }                        var len:int = string.length;            var i:int = str.lastIndexOf(string);                        if(i == -1){                this.caratIndex = txt.length;                i = txt.lastIndexOf(string);            }                        if(i == -1){                return -1;            }                        this.caratIndex = i;                        return i;        }	}}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?