5 Replies Latest reply on May 13, 2009 1:57 PM by BennyBeta

    last line of textarea

    ShaF10 Level 1

      Hi Guys

       

      Is there any way I can get the last line of a fixed size textarea ?

        • 1. Re: last line of textarea
          BennyBeta Level 4

          This should do it:

           

          // get the textArea's textField
          use namespace mx_internal;

          var textField:IUITextField = textArea.getTextField();

           

          // get the number of lines in the textArea

          var numLines:Number = textField.numLines

           

          // get the last line of text

          var lastLine:String = textField.getLineText( numLines-1 );

           

          Let me know if that does the trick...


          Ben Edwards

          1 person found this helpful
          • 2. Re: last line of textarea
            ShaF10 Level 1

            Thanks, that works, there are afew problems through.

             

            1. For some strange reason I need to press the right arrow key for the code to read the last line.

            2. I have written a function to remove the text from the last line. Is there a better way I could have coded it ?

             

            Please take a look at the code below (note: this is a simplified version of the actual code):

             

            private function taKeyDownHandler(event:KeyboardEvent):void {
                var ta:TextArea = event.currentTarget as TextArea;
                var taCurrent:TextArea = null;
                var taIndex:int = 0;
                var taNext:TextArea = null;
                            
                // create new textarea if one not found
                if(!(taNext is TextArea)) {
                    taNew(); // create new textarea
                }
                // or append current textarea lastline to next textarea's firstline
                else {
                    // get the textArea's textField
                    use namespace mx_internal;
                    var textField:IUITextField = ta.getTextField();
            
                    // get the number of lines in the textArea
                    var numLines:Number = textField.numLines;
            
                    // get the last line of text
                    var lastLine:String = textField.getLineText(numLines - 1);
                    
                    // get next textarea
                    taCurrent = event.currentTarget as TextArea;
                    taIndex = taContainer.getChildIndex(taCurrent);
                    taNext = taContainer.getChildAt(taIndex + 1) as TextArea;
                    
                    // Concatenate lastline to firstline of next textarae
                    taNext.text = (lastLine + taNext.text)
                    
                    // remove lastline from predeccesor textarea
                    ta.text = str_replace("", lastLine, ta.text);
                    
                }
            }
            
            private function str_replace( replace_with:String, replace:String, original:String ):String {
                 return original.replace(replace, replace_with)
            }
            
            • 3. Re: last line of textarea
              BennyBeta Level 4

              I'm not sure why you are using the keyDown event, maybe try listening for the "change" event.

               

              A better way to remove the last line, maybe try removing the last x characters of the textArea.text where x is the length of the last line.

              1 person found this helpful
              • 4. Re: last line of textarea
                ShaF10 Level 1

                I was listening for certain keycodes.

                 

                Can you give me an example of how you would implement it ?

                • 5. Re: last line of textarea
                  BennyBeta Level 4

                  Try this:

                  ta.text = ta.text.substr( 0, ta.text.length-lastLine.length );