TextField

TextField is a DisplayObject for displaying and editing text. It provides text-related functionality from label display to input forms.

Inheritance

classDiagram
    DisplayObject <|-- InteractiveObject
    InteractiveObject <|-- TextField

    class TextField {
        +text: String
        +textColor: Number
        +type: String
        +setTextFormat()
    }

Properties

PropertyTypeDescription
textstringA string that is the current text in the text field
htmlTextstringContains the HTML representation of the text field contents
lengthnumberThe number of characters in a text field (read-only)
maxCharsnumberThe maximum number of characters that the text field can contain (0 for unlimited)
restrictstringIndicates the set of characters that a user can enter into the text field
defaultTextFormatTextFormatSpecifies the formatting to be applied to the text
stopIndexnumberSetting an arbitrary display end position for text (default: -1)
PropertyTypeDescription
widthnumberIndicates the width of the display object, in pixels
heightnumberIndicates the height of the display object, in pixels
textWidthnumberThe width of the text in pixels (read-only)
textHeightnumberThe height of the text in pixels (read-only)
autoSizestringControls automatic sizing and alignment of text fields (“none”, “left”, “center”, “right”)
autoFontSizebooleanControls automatic sizing and alignment of text size (default: false)
wordWrapbooleanA Boolean value that indicates whether the text field has word wrap (default: false)
multilinebooleanIndicates whether field is a multiline text field (default: false)
numLinesnumberNumber of text lines (read-only)
PropertyTypeDescription
backgroundbooleanSpecifies whether the text field has a background fill (default: false)
backgroundColornumberThe color of the text field background (default: 0xffffff)
borderbooleanSpecifies whether the text field has a border (default: false)
borderColornumberThe color of the text field border (default: 0x000000)
PropertyTypeDescription
thicknessnumberThe text width of the outline, which can be disabled with 0 (default: 0)
thicknessColornumberThe color of the outline text in hexadecimal format (default: 0)
PropertyTypeDescription
typestringThe type of the text field (“static”, “dynamic”, “input”) (default: “static”)
focusbooleanWhether the text field has focus (default: false)
focusVisiblebooleanControls the visibility of the text field’s blinking line (default: false)
focusIndexnumberIndex of the focus position of the text field (default: -1)
selectIndexnumberIndex of the selected position of the text field (default: -1)
compositionStartIndexnumberComposition start index of the text field (default: -1)
compositionEndIndexnumberComposition end index of the text field (default: -1)
PropertyTypeDescription
scrollXnumberScroll position on the x-axis (default: 0)
scrollYnumberScroll position on the y-axis (default: 0)
scrollEnabledbooleanControl ON/OFF of the scroll function (default: true)
xScrollShapeShapeShape object for x scroll bar display (read-only)
yScrollShapeShapeShape object for y scroll bar display (read-only)

Methods

MethodReturnDescription
appendText(newText: string)voidAppends the string specified by the newText parameter to the end of the text of the text field
insertText(newText: string)voidAdds text to the focus position of the text field
deleteText()voidDeletes the selection range of the text field
getLineText(lineIndex: number)stringReturns the text of the line specified by the lineIndex parameter
replaceText(newText: string, beginIndex: number, endIndex: number)voidReplaces the range of characters that the beginIndex and endIndex parameters specify with the contents of the newText parameter
selectAll()voidSelects all text in the text field
copy()voidCopy a selection of text fields
paste()voidPaste the copied text into the selected range
setFocusIndex(stageX: number, stageY: number, selected?: boolean)voidSets the focus position of the text field
keyDown(event: KeyboardEvent)voidProcesses the key down event

TextFormat

A class for setting text styles.

Properties

PropertyTypeDescription
fontStringFont name
sizeNumberFont size
colorNumberText color
boldBooleanBold
italicBooleanItalic
alignStringAlignment (“left”, “center”, “right”)
leadingNumberLine spacing (pixels)
letterSpacingNumberLetter spacing (pixels)

Usage Examples

Basic Text Display

const { TextField } = next2d.text;

const textField = new TextField();
textField.text = "Hello, Next2D!";
textField.x = 100;
textField.y = 100;

stage.addChild(textField);

Applying TextFormat

const { TextField, TextFormat } = next2d.text;

const textField = new TextField();
textField.text = "Styled Text";

// Create TextFormat
const format = new TextFormat();
format.font = "Arial";
format.size = 24;
format.color = 0x3498db;
format.bold = true;

// Apply format
textField.setTextFormat(format);

// Set as default format
textField.defaultTextFormat = format;

stage.addChild(textField);

Auto Size

const { TextField } = next2d.text;

const textField = new TextField();
textField.autoSize = "left";  // Auto expand to fit text
textField.text = "This text will auto-size the field";

stage.addChild(textField);

Multiline Text

const { TextField } = next2d.text;

const textField = new TextField();
textField.width = 200;
textField.multiline = true;
textField.wordWrap = true;
textField.text = "This is multiline text. It will wrap automatically.";

stage.addChild(textField);

Input Field

const { TextField } = next2d.text;

const inputField = new TextField();
inputField.type = "input";
inputField.width = 200;
inputField.height = 30;
inputField.border = true;
inputField.borderColor = 0xcccccc;
inputField.background = true;
inputField.backgroundColor = 0xffffff;

// Placeholder alternative
inputField.text = "";

// Input restriction (numbers only)
inputField.restrict = "0-9";

// Input event
inputField.addEventListener("change", function(event) {
    console.log("Input value:", event.target.text);
});

stage.addChild(inputField);

Password Field

const { TextField } = next2d.text;

const passwordField = new TextField();
passwordField.type = "input";
passwordField.displayAsPassword = true;
passwordField.width = 200;
passwordField.height = 30;
passwordField.border = true;
passwordField.borderColor = 0xcccccc;

stage.addChild(passwordField);

HTML Text

const { TextField } = next2d.text;

const textField = new TextField();
textField.width = 300;
textField.multiline = true;
textField.htmlText = '<font face="Arial" size="20" color="#3498db">' +
    '<b>Bold Text</b><br/>' +
    '<i>Italic Text</i><br/>' +
    '<font color="#e74c3c">Red Text</font>' +
    '</font>';

stage.addChild(textField);

Scrollable Text

const { TextField } = next2d.text;

const textField = new TextField();
textField.width = 200;
textField.height = 100;
textField.multiline = true;
textField.wordWrap = true;
textField.border = true;
textField.text = "Long text...\n".repeat(20);

// Scroll operations
function scrollUp() {
    if (textField.scrollY > 0) {
        textField.scrollY -= 10;
    }
}

function scrollDown() {
    textField.scrollY += 10;
}

stage.addChild(textField);

Dynamic Text Update

const { TextField, TextFormat } = next2d.text;

const scoreField = new TextField();
scoreField.autoSize = "left";

const format = new TextFormat();
format.font = "Arial";
format.size = 32;
format.color = 0xffffff;
scoreField.defaultTextFormat = format;

let score = 0;

function updateScore(points) {
    score += points;
    scoreField.text = "Score: " + score;
}

updateScore(0);
stage.addChild(scoreField);

Text Outline Effect

const { TextField, TextFormat } = next2d.text;

const textField = new TextField();
textField.autoSize = "left";

const format = new TextFormat();
format.font = "Arial";
format.size = 48;
format.color = 0xffffff;
textField.defaultTextFormat = format;

textField.text = "Outlined Text";
textField.thickness = 2;
textField.thicknessColor = 0x000000;

stage.addChild(textField);

Replace Part of Text

const { TextField } = next2d.text;

const textField = new TextField();
textField.autoSize = "left";
textField.text = "Hello World!";

// Replace "World" with "Next2D"
textField.replaceText("Next2D", 6, 11);
// Result: "Hello Next2D!"

stage.addChild(textField);

Events

EventDescription
changeWhen text is changed
focusWhen focus is gained
blurWhen focus is lost
keyDownWhen key is pressed
keyUpWhen key is released
const { TextField } = next2d.text;

const inputField = new TextField();
inputField.type = "input";

// Submit form on Enter key
inputField.addEventListener("keyDown", function(event) {
    if (event.keyCode === 13) {  // Enter
        submitForm(inputField.text);
    }
});

stage.addChild(inputField);