Allowed characters number and disabling textfield scrolling

How can we do if we have an "input" textfield and we wish to bind the max characters without making our textfield scrolling?
Think for example that we've only 4 visible lines for a textField t. How to prevent that users will write or just paste more text overfolwing on line 5?

below the solution I adopted:

1 I add a custom textfield with some height;
the first trouble is how many line the textfield is if this text is empty? (i just add a recursive function for understand this) check at getLastVisibleLine.
2 I slice each character under the last visible line.
    import flash.text.TextField;
    import flash.text.TextFieldType;
    import flash.text.TextFormat;
    public class TextFieldUtil extends Sprite {
        public function TextFieldUtil()    { }
        //undestand how many lines we can insert in a textField without scrolling
        public static function getLastVisibleLine(t:TextField):int {
            t.type = TextFieldType.INPUT;
            var current_string:String = t.text;
            t.text = "";
            var l:int = 0;
            var scrld:Boolean = false;
            t.addEventListener(Event.SCROLL, checkScrl);
            function checkScrl(e:Event) {
       = 0;
                t.text = current_string;
                scrld = true;
            while (scrld == false) {
            t.removeEventListener(Event.SCROLL, checkScrl);
            return l;
        public static function createCustomTextField(x:Number, y:Number, width:Number, height:Number):TextField {
            var t:TextField = new TextField();
            t.type = TextFieldType.INPUT;
            t.mouseWheelEnabled = false
            t.wordWrap = true;
            t.multiline = true;
            t.x = x;
            t.y = y;
            t.width = width;
            t.height = height;
            //t.background = true;
            //t.border = true;
            var visiblesLines:int = getLastVisibleLine(t);
            t.addEventListener(Event.CHANGE, function (e:Event) { // lock text insertion and delete exceeding characters.
                var t:TextField = as TextField;
                if (t.numLines > visiblesLines)    {
                    t.type = "dynamic";
                while (t.numLines > visiblesLines){
                    var current_string:String = t.text;
                    t.text = current_string.slice(0, -1);
                t.type = "input"; 
            return t;
I wrote also a version with styles and special textFormat. Maybe I'll post in the future.


Posta un commento