Skip to main content
Participating Frequently
February 3, 2011
Answered

How to copy some line and its textFormat from textField

  • February 3, 2011
  • 4 replies
  • 930 views

the example of my problem is:

   I have 2 textFiled  there are:

                       1. myTextFied   ( to store the original text )

                       2. displayText     ( to show some line of  original text )

   I try this:

   myTextField.htmlText = '<B>THIS IS A TEXT LINE 1</B>\n' +

                                       '<I>THIS IS  A TEXT LINE 2</I>\n' +

                                       '<U>THIS IS  A TEXT LINE 3</U>';

      myTextField is  narrow  and I set myTextField.wordWrap =   true;

      So the result of myTextField display like this: 

                                                             THIS IS A TEXT

                                                             LINE 1

                                                            THIS IS A TEXT

                                                             LINE 2

                                                             THIS IS A TEXT

                                                             LINE 3

      and I use displayText to get  text of line 2 and line 3 from myTextField:

       so i try this:

                                             displayText.htmlText =  myTextField.getLineText(1);

                                             displayText.htmlText += myTextField.getLineText(2);

     but the result of  displayText  like this:

                                                              LINE 1 THIS IS A TEXT

       it not same the original textFormat.

        I want to make it to display like this

                                                             LINE 1

                                                             THIS IS A TEXT

                        How to find it out. Can you guy help me please!!!

  Thank you

and sorry about my English.

This topic has been closed for replies.
Correct answer

after you added text in displayText you can get format of a line in myTextField and then set it to the respective line of displayText:

var fmt = myTextField.getTextFormat(lineStartIndex, lineEndIndex);

displayText.setTextFormat(fmt, start, end);

4 replies

tonsmart5Author
Participating Frequently
February 3, 2011

็Thank you

tonsmart5Author
Participating Frequently
February 3, 2011

Thank  Andrei1 to remind me.

and thank Eugeny89 , your answer is  work !!  I'm very glad.

               and I still have  more a bit problem, that is:

                  myTextFied display like this;

                                         THIS IS A TEXT

                                         LINE 1 AND NEXT

                                         THIS IS A TEXT

                                         LINE 2 AND NEXT

                                         THIS IS A TEXT

                                         LINE 3

                that mean  1 line have 2 style

                And I want to get text of line 2 and make it to display like this.

                                   LINE 1 AND NEXT

                I try to use this from your approach .

                          var  lineStartIndex:int = myTextField.getLineOffset(1);

                           var lineEndIndex:int  =       myTextField.getLineOffset(1)+myTextField.getLineLength(1);

                      var fmt = myTextField.getTextFormat( lineStartIndex, lineEndIndex);

                      displayText.setTextFormat(fmt, displayText.getLineOffset(0), displayText.getLineOffset(0)+displayText.getLineLength(0));

               then it display like this :

                               LINE 1 AND NEXT

             So it possible to get 2 or more original textFormat from 1 line to display like this;

                              LINE 1 AND NEXT

February 3, 2011

see

http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/text/TextField.html#setTextFormat()

beginIndex and endIndex in setTextFormat are not obliget to be index of start/end of the line. Just do something like:

var txt = "LINE 1"

                      var  startIndex:int = myTextField.getLineOffset(1);

                      var endIndex:int  =       myTextField.getLineOffset(1)+txt.length;

                      var fmt = myTextField.getTextFormat( startIndex, endIndex);

                      displayText.setTextFormat(fmt, displayText.getLineOffset(0), displayText.getLineOffset(0)+txt.length);

well, I gues yiu caught the idea.

Inspiring
February 3, 2011

Lines are 0-based (as all enumerations on AS3). SO, line 1 is 0, line 2 is 1, etc.

Correct answer
February 3, 2011

after you added text in displayText you can get format of a line in myTextField and then set it to the respective line of displayText:

var fmt = myTextField.getTextFormat(lineStartIndex, lineEndIndex);

displayText.setTextFormat(fmt, start, end);