Flash / Flex / ActionScript/TextField/text — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 09:19, 26 мая 2010
Содержание
- 1 Append text to TextField
- 2 Displaying Text
- 3 If beginIndex and endIndex are equal, the newText is inserted immediately before the specified beginIndex
- 4 Replaces the characters "bcd" in the text "abcde" with the new text "x"
- 5 Retrieving Strings from a TextField
- 6 Special characters, such as \t for tab and \n for newline, can be used within a text string.
- 7 The getLineIndexOfChar()/getLineLength()
- 8 The url property allows you to add a hyperlink to text.
- 9 You can append text by using the += operator or the appendText( ) method
Append text to TextField
package {
import flash.display.Sprite;
import flash.text.TextField;
public class Main extends Sprite {
public function Main( ) {
var field:TextField = new TextField( );
field.autoSize = flash.text.TextFieldAutoSize.LEFT;
field.text = "A";
field.text += "\n";
field.text += "B."; addChild(field);
}
}
}
Displaying Text
package {
import flash.display.Sprite;
import flash.text.TextField;
public class Main extends Sprite {
public function Main( ) {
var field:TextField = new TextField( );
field.type = flash.text.TextFieldType.INPUT;
field.border = true;
field.background = true;
field.text = "this will display in the field";
addChild(field);
}
}
}
If beginIndex and endIndex are equal, the newText is inserted immediately before the specified beginIndex
package{
import flash.display.Sprite;
import flash.text.*;
public class Main extends Sprite{
public function Main(){
var t:TextField = new TextField( );
t.text = "mat";
t.replaceText(2, 2, "s");
trace(t.text); // Displays: mast
addChild(t);
}
}
}
Replaces the characters "bcd" in the text "abcde" with the new text "x"
package{
import flash.display.Sprite;
import flash.text.*;
public class Main extends Sprite{
public function Main(){
var t:TextField = new TextField( );
t.text = "abcde";
t.replaceText(1, 4, "x");
trace(t.text); // Displays: axe
addChild(t);
}
}
}
Retrieving Strings from a TextField
package {
import flash.text.TextField;
import flash.events.MouseEvent;
import flash.display.Sprite;
public class WidthHeightTest extends Sprite
{
private var txt:TextField;
public function WidthHeightTest()
{
txt = new TextField();
txt.autoSize = "left";
addChild(txt);
txt.text = "conductor.";
txt.addEventListener(MouseEvent.MOUSE_MOVE, getTextUnderMouse);
}
private function getTextUnderMouse(mouseEvent:MouseEvent):void
{
var charInt:int = txt.getCharIndexAtPoint(mouseEvent.stageX, mouseEvent.stageY);
trace(txt.text.charAt(charInt));
}
}
}
Special characters, such as \t for tab and \n for newline, can be used within a text string.
package {
import flash.display.Sprite;
import flash.text.TextField;
public class Main extends Sprite {
public function Main( ) {
var field:TextField = new TextField( );
field.text = "this will \n display in the field";
addChild(field);
}
}
}
The getLineIndexOfChar()/getLineLength()
package
{
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.events.TextEvent;
public class Main extends Sprite
{
private var input:TextField;
private var text:TextField;
private var regex:RegExp;
private var selectedFormat:TextFormat;
private var defaultFormat:TextFormat;
public function Main()
{
defaultFormat = new TextFormat();
defaultFormat.bold = false;
defaultFormat.color = 0x00000;
selectedFormat = new TextFormat();
selectedFormat.color = 0xFF0000;
selectedFormat.bold = true;
input = new TextField();
input.type = "input";
input.border = true;
input.height = 20;
addChild(input);
input.addEventListener(TextEvent.TEXT_INPUT, checkInput);
text = new TextField();
text.text = "development.";
text.wordWrap = true;
text.multiline = true;
addChild(text);
text.y = 200;
}
private function checkInput(textEvent:TextEvent):void
{
text.setTextFormat(defaultFormat);
var indexOfInt:int;
var lineIndex:int;
var firstChar:int;
var lastChar:int;
if((indexOfInt = text.text.indexOf(input.text)) != -1)
{
lineIndex = text.getLineIndexOfChar(indexOfInt);
firstChar = text.getLineOffset(lineIndex);
lastChar = firstChar + text.getLineLength(lineIndex);
text.setTextFormat(selectedFormat, firstChar, lastChar);
}
}
}
}
The url property allows you to add a hyperlink to text.
package{
import flash.display.Sprite;
import flash.text.*;
public class Main extends Sprite{
public function Main(){
var tContent:TextField = new TextField();
tContent.multiline = true;
tContent.border = true;
tContent.wordWrap = true;
tContent.text = "Visit the Web site";
var tfFormatter:TextFormat = new TextFormat();
tfFormatter.url = "http://thefactoryfactoryfactory.ru/wordpress";
tfFormatter.target = "_blank";
tfFormatter.underline = true;
tContent.setTextFormat(tfFormatter,2,5);
addChild(tContent);
}
}
}
You can append text by using the += operator or the appendText( ) method
package {
import flash.display.Sprite;
import flash.text.TextField;
public class Main extends Sprite {
public function Main( ) {
var field:TextField = new TextField( );
field.appendText("new text");
addChild(field);
}
}
}