How To Append text with Actionscript 3
appendText () method
public function appendText(newText:String):void
Language Version : ActionScript 3.0
Runtime Versions : AIR 1.0, Flash Player 9
Appends the string specified by the newText parameter to the end of the text of the text field. This method is more efficient than an addition assignment (+=) on a text property (such as someTextField.text += moreText), particularly for a text field that contains a significant amount of content.
Parameters
newText:String — The string to append to the existing text. |
Example
The following example displays the time if it's not the weekend or the text, "It's the weekend," if it is. It also counts the number of characters up to a certain position and the number of lines in the text field.The outputText text field is set to automatically fit the text and to resize as a left-justified text using autoSize property. The outputText.text property writes the first line of the content and the method appendText() appends the rest of the content. (It is not necessary to start with the text property. The appendText() method could also be used to append text from the outset.) Setting the text property a second time will overwrite the original text. Use += operator to append content with the text property.
The if statement checks if the date is Saturday (6) or Sunday (0). If it's not, the toLocaleTimeString() method returns the local time, which is appended to the text field's content.
The text field's length property is used to read the number of characters until right before the function is called, and the property numLines is used to count the number of lines in the text field. Note that the empty lines are counted in the number of lines and the empty spaces and line breaks (\n) are counted in determining the content length.
package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
public class TextField_appendTextExample extends Sprite {
public function TextField_appendTextExample() {
var outputText:TextField = new TextField();
var today:Date = new Date();
outputText.x = 10;
outputText.y = 10;
outputText.background = true;
outputText.autoSize = TextFieldAutoSize.LEFT;
outputText.text = "WHAT TIME IS IT?" + "\n\n";
if((today.day == 0) || (today.day == 6)) {
outputText.appendText("It's the weekend.");
outputText.appendText("\n\n");
} else {
outputText.appendText("The time is: ");
outputText.appendText(today.toLocaleTimeString() + ".\n\n");
}
outputText.appendText("Number of characters including line breaks and spaces so far: ");
outputText.appendText(outputText.length.toString() + "\n");
outputText.appendText("Number of lines in the outputText: ");
outputText.appendText(outputText.numLines.toString());
this.addChild(outputText);
}
}
}
An example of a bad way to append text to a textbox (or just to a variable)
myVariable = myVariable + "there"
Which outputs hey there, BUT I will recommend you doing it another way.
This is what we will do:
The method we will use is called appendText, but first we need to set up a button and some textboxes, and give them instance names.
First make the three buttons as shown in the example above, give them the following instance names. "appendBreakBtn" "appendBtn" "breakBtn"
Now make two textboxes, you can just drag them from the component panel, make one of them multline and give them the following instance names. "inputText" and "displayText".
Now we are ready to look at some code.
This first function will just add functionality to the appendBtn button, to add some text into the textbox named displayText.
We add an eventlistener to listen to when somebody clicks the button, then active the function to add the text from the input text field.
appendBtn.addEventListener(MouseEvent.CLICK, doAppend);
function doAppend(evt:MouseEvent):void {
displayText.appendText(inputText.text);
}
This next function will be to add a line break, because for some reason this is really weird with textboxes in flash actionscript, what you have to do is to add a /n this is a line break. So let's do that.
breakBtn.addEventListener(MouseEvent.CLICK, doBreak);
function doBreak(evt:MouseEvent):void {
displayText.appendText("\n");
}
Finally the last thing we will do is to combine those two functions so after we append some text to the textfield, we will add a linebreak.
appendBreakBtn.addEventListener(MouseEvent.CLICK, doAppendBreak);
function doAppendBreak(evt:MouseEvent):void {
displayText.appendText(inputText.text + "\n");
}
As you might see, this is the first step towards making a small chat in flash, so maybe I will make an article about that, and this will be a pre launch of that, who knows.
Similar Posts:
- How to Add scrolls to textboxes with ActionScript 3.0
- link button with actionscript 3
- Flash AS3 simplebutton
- Flash Actionscript getURL
- Easy Scrolling text
- How To Apply filters with AS3
- Create MovieClip Scroller
- How to Create and edit text fields
- Flash Digital clock
- Preloader actionscript 3