Textarea - 每行限制内容

时间:2011-07-01 07:45:34

标签: flex actionscript flex-spark

无论如何我可以限制每行文本区域的内容吗?

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

欢迎任何帮助。

非常感谢

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

适用于\r

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