2011-07-01 72 views
0

是否有我可以限制每行文本區域的內容?Textarea - 每行限制內容

例如,如果用戶粘貼超出「預定義」行數的文本,textarea將只顯示內容,直到先前設置的限制值。請記住,空行應該被計數。

任何幫助將受到歡迎。

非常感謝您

回答

0

嘗試了這一點:

<?xml version="1.0" encoding="utf-8"?> 
<s:Application name="Spark_TextArea_limit_lines" 
       xmlns:fx="http://ns.adobe.com/mxml/2009" 
       xmlns:s="library://ns.adobe.com/flex/spark" 
       xmlns:mx="library://ns.adobe.com/flex/mx"> 

    <fx:Script> 
     <![CDATA[ 

      private const MAX_LINES:uint = 5; 

      private function textChangeHandler(event:Event):void { 
       //Split the string into an array of lines 
       var lines:Array = textArea.text.split("\n"); 
       //If there are too many lines... 
       if (lines.length > MAX_LINES) { 
        //Clear the existing text 
        textArea.text = ""; 
        //Then insert MAX_LINES of the previous text 
        for (var i:uint=0; i<MAX_LINES; i++) { 
         textArea.text += lines[i] + "\n"; 
        } 
        //Finally, move the cursor to the end of input, as 
        //it is reset to position 0 when the text is modified. 
        textArea.selectRange(textArea.text.length, textArea.text.length); 
       } 
      } 

     ]]> 
    </fx:Script> 

    <s:TextArea id="textArea" change="textChangeHandler(event)"/> 

</s:Application> 
+0

工作這種方法的問題是,不守空行。 例如,在這種情況下,所有行將被連接。 1行 行2 行3 – Edias

+0

這種方法的問題是,不保留空行。 例如,在這種情況下,所有行將被連接。 1行 行2 行3 – Edias

0

\r

var lines:Array = textArea.text.split("\r");