【问题标题】:Textarea - Limit content per lineTextarea - 每行限制内容
【发布时间】:2011-07-01 07:45:34
【问题描述】:

我是否可以限制每行文本区域的内容?

例如,如果用户粘贴的文本超出“预定义”行数,则文本区域将仅显示内容,直到先前设置的限制值。请记住,应该计算空行。

欢迎任何帮助。

非常感谢

【问题讨论】:

    标签: apache-flex actionscript flex-spark


    【解决方案1】:

    试试这个:

    <?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>
    

    【讨论】:

    • 这种方法的问题是不会保留空行。例如,在这种情况下,所有行都将被连接。第1行第2行第3行
    • 这种方法的问题是不会保留空行。例如,在这种情况下,所有行都将被连接。第1行第2行第3行
    【解决方案2】:

    它适用于\r

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-20
      • 2018-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-15
      相关资源
      最近更新 更多