TextField

TextFieldは、テキストの表示と編集を行うDisplayObjectです。ラベル表示から入力フォームまで、テキスト関連の機能を提供します。

継承関係

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

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

プロパティ

テキスト関連

プロパティ説明
textstringテキストフィールド内の現在のテキストであるストリング
htmlTextstringテキストフィールドの内容をHTMLで表した文字列
lengthnumberテキストフィールド内の文字数(読み取り専用)
maxCharsnumberユーザーが入力できる最大文字数(0で無制限)
restrictstringユーザーがテキストフィールドに入力できる文字のセットを指定
defaultTextFormatTextFormatテキストに適用するデフォルトのフォーマット
stopIndexnumberテキストの任意の表示終了位置の設定(デフォルト: -1)

表示関連

プロパティ説明
widthnumber表示オブジェクトの幅(ピクセル単位)
heightnumber表示オブジェクトの高さ(ピクセル単位)
textWidthnumberテキストの幅(ピクセル単位、読み取り専用)
textHeightnumberテキストの高さ(ピクセル単位、読み取り専用)
autoSizestringテキストフィールドの自動的な拡大/縮小および整列を制御(“none”, “left”, “center”, “right”)
autoFontSizebooleanテキストサイズの自動的な拡大/縮小および整列を制御(デフォルト: false)
wordWrapbooleanテキストフィールドのテキストを折り返すかどうか(デフォルト: false)
multilineboolean複数行テキストフィールドであるかどうか(デフォルト: false)
numLinesnumberテキストの行数(読み取り専用)

境界線・背景関連

プロパティ説明
backgroundbooleanテキストフィールドに背景の塗りつぶしがあるかどうか(デフォルト: false)
backgroundColornumberテキストフィールドの背景の色(デフォルト: 0xffffff)
borderbooleanテキストフィールドに境界線があるかどうか(デフォルト: false)
borderColornumberテキストフィールドの境界線の色(デフォルト: 0x000000)

輪郭関連

プロパティ説明
thicknessnumber輪郭のテキスト幅。0(デフォルト値)で無効
thicknessColornumber輪郭のテキストの色(16進数形式、デフォルト: 0)

入力関連

プロパティ説明
typestringテキストフィールドのタイプ(“static”, “dynamic”, “input”)(デフォルト: “static”)
focusbooleanテキストフィールドがフォーカスを持つかどうか(デフォルト: false)
focusVisiblebooleanテキストフィールドの点滅線の表示・非表示を制御(デフォルト: false)
focusIndexnumberテキストフィールドのフォーカス位置のインデックス(デフォルト: -1)
selectIndexnumberテキストフィールドの選択位置のインデックス(デフォルト: -1)
compositionStartIndexnumberテキストフィールドのコンポジション開始インデックス(デフォルト: -1)
compositionEndIndexnumberテキストフィールドのコンポジション終了インデックス(デフォルト: -1)

スクロール関連

プロパティ説明
scrollXnumberx軸のスクロール位置(デフォルト: 0)
scrollYnumbery軸のスクロール位置(デフォルト: 0)
scrollEnabledbooleanスクロール機能のON/OFFの制御(デフォルト: true)
xScrollShapeShapexスクロールバーの表示用のShapeオブジェクト(読み取り専用)
yScrollShapeShapeyスクロールバーの表示用のShapeオブジェクト(読み取り専用)

メソッド

メソッド戻り値説明
appendText(newText: string)void指定されたストリングをテキストフィールドのテキストの最後に付加します
insertText(newText: string)voidテキストフィールドのフォーカス位置にテキストを追加します
deleteText()voidテキストフィールドの選択範囲を削除します
getLineText(lineIndex: number)string指定された行のテキストを返します
replaceText(newText: string, beginIndex: number, endIndex: number)void指定された文字範囲を新しいテキストの内容に置き換えます
selectAll()voidテキストフィールドのすべてのテキストを選択します
copy()voidテキストフィールドの選択範囲をコピーします
paste()voidコピーしたテキストを選択範囲に貼り付けます
setFocusIndex(stageX: number, stageY: number, selected?: boolean)voidテキストフィールドのフォーカス位置を設定します
keyDown(event: KeyboardEvent)voidキーダウンイベントを処理します

TextFormat

テキストのスタイルを設定するクラスです。

プロパティ

プロパティ説明
fontStringフォント名
sizeNumberフォントサイズ
colorNumberテキスト色
boldBoolean太字
italicBoolean斜体
alignString配置(“left”, “center”, “right”)
leadingNumber行間(ピクセル)
letterSpacingNumber文字間隔(ピクセル)

使用例

基本的なテキスト表示

const { TextField } = next2d.text;

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

stage.addChild(textField);

TextFormatの適用

const { TextField, TextFormat } = next2d.text;

const textField = new TextField();
textField.text = "スタイル付きテキスト";

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

// フォーマットを適用
textField.setTextFormat(format);

// デフォルトフォーマットとして設定
textField.defaultTextFormat = format;

stage.addChild(textField);

自動サイズ調整

const { TextField } = next2d.text;

const textField = new TextField();
textField.autoSize = "left";  // テキストに合わせて自動拡張
textField.text = "このテキストに合わせてサイズが調整されます";

stage.addChild(textField);

複数行テキスト

const { TextField } = next2d.text;

const textField = new TextField();
textField.width = 200;
textField.multiline = true;
textField.wordWrap = true;
textField.text = "これは複数行のテキストです。自動的に折り返されます。";

stage.addChild(textField);

入力フィールド

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;

// プレースホルダーの代わり
inputField.text = "";

// 入力制限(数字のみ)
inputField.restrict = "0-9";

// 入力イベント
inputField.addEventListener("change", (event) => {
    console.log("入力値:", inputField.text);
});

stage.addChild(inputField);

パスワードフィールド

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テキスト

const { TextField } = next2d.text;

const textField = new TextField();
textField.width = 300;
textField.multiline = true;
textField.htmlText = `
<font face="Arial" size="20" color="#3498db">
  <b>太字テキスト</b><br/>
  <i>斜体テキスト</i><br/>
  <font color="#e74c3c">赤いテキスト</font>
</font>
`;

stage.addChild(textField);

スクロール可能なテキスト

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 = "長いテキスト...\n".repeat(20);

// スクロール操作
function scrollUp() {
    if (textField.scrollY > 0) {
        textField.scrollY -= 10;
    }
}

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

stage.addChild(textField);

動的なテキスト更新

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);

テキストの輪郭効果

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 = "輪郭付きテキスト";
textField.thickness = 2;
textField.thicknessColor = 0x000000;

stage.addChild(textField);

テキストの一部置換

const { TextField } = next2d.text;

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

// "World"を"Next2D"に置き換え
textField.replaceText("Next2D", 6, 11);
// 結果: "Hello Next2D!"

stage.addChild(textField);

イベント

イベント説明
changeテキストが変更されたとき
focusフォーカスを得たとき
blurフォーカスを失ったとき
keyDownキーが押されたとき
keyUpキーが離されたとき
const { TextField } = next2d.text;

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

// Enterキーでフォーム送信
inputField.addEventListener("keyDown", (event) => {
    if (event.keyCode === 13) {  // Enter
        submitForm(inputField.text);
    }
});

stage.addChild(inputField);

関連項目