Text
Textは、文字列を画面に表示するためのノードです。フォント、色、配置、ボーダーなど、豊富なテキスト表示オプションを提供し、ゲームやアプリケーションでのテキスト表示に必要な機能を備えています。
public class GameScene : Scene{ public override void OnStart() { // シンプルなテキスト(デフォルトフォント、白色) var text = new Text("Hello, World!"); Root.Add(text);
// フォントとカラーを指定 var styledText = new Text("Styled Text", Font.GetDefault(24), Color.Red) .Location(10, 50); Root.Add(styledText); }}テキストのプロパティ
Section titled “テキストのプロパティ”基本プロパティ
Section titled “基本プロパティ”テキスト内容
Section titled “テキスト内容”var text = new Text("初期テキスト");
// テキストを変更text.Content = "新しいテキスト";
// 動的なテキスト更新text.Content = $"スコア: {score}";// デフォルトフォントvar defaultText = new Text("Default Font", Font.GetDefault());
// サイズ指定var largeText = new Text("Large Text", Font.GetDefault(32));
// カスタムフォントvar customText = new Text("Custom Font", Font.FromFile("assets/MyFont.ttf", 24));// 基本色text.Color = Color.Red;text.Color = Color.Blue;text.Color = Color.Green;
// カスタムカラーtext.Color = Color.FromArgb(255, 200, 100, 50);
// 半透明text.Color = Color.FromArgb(128, 255, 255, 255);テキストに縁取り(ボーダー)を追加できます。BorderColor を指定すると縁取りが有効になり、BorderThickness で太さを変更できます。
var outlinedText = new Text("Outlined Text");outlinedText.BorderColor = Color.Black;outlinedText.BorderThickness = 2;
// 太いボーダーvar thickBorderText = new Text("Thick Border");thickBorderText.Color = Color.White;thickBorderText.BorderColor = Color.DarkBlue;thickBorderText.BorderThickness = 4;
// 縁取りを無効にするoutlinedText.BorderColor = null;配置とレイアウト
Section titled “配置とレイアウト”// 左揃え(デフォルト)text.HorizontalAlignment = HorizontalAlignment.Left;
// 中央揃えtext.HorizontalAlignment = HorizontalAlignment.Center;
// 右揃えtext.HorizontalAlignment = HorizontalAlignment.Right;// 上揃え(デフォルト)text.VerticalAlignment = VerticalAlignment.Top;
// 中央揃えtext.VerticalAlignment = VerticalAlignment.Center;
// 下揃えtext.VerticalAlignment = VerticalAlignment.Bottom;サイズと折り返し
Section titled “サイズと折り返し”PreferredSize でテキストを配置する領域の大きさを、WrapMode で折り返し方を指定します。
var longText = new Text("これは非常に長いテキストで、指定された幅で自動的に折り返されます。");longText.PreferredSize = (200, 100); // 200x100の領域に表示longText.WrapMode = WrapMode.Mixed; // 自動折り返しを有効化
Root.Add(longText);WrapMode には次の値を指定できます。
WrapMode.None
折り返しません(デフォルト)。改行文字の位置でのみ改行されますWrapMode.Character
幅を超えた位置の文字で折り返します。単語の途中でも改行されますWrapMode.Word
単語の区切りで折り返します。英語のテキストに向いていますWrapMode.Mixed
日本語は任意の文字で、英語は単語の区切りで折り返します。日本語と英語が混ざったテキストに向いています
PreferredSize を (0, 0) にすると、テキストが収まる大きさに自動で調整されます。
var multilineText = new Text("Line 1\nLine 2\nLine 3");multilineText.LineSpacing = 1.5f; // 1.5倍の行間隔リッチテキスト
Section titled “リッチテキスト”部分的に文字装飾が可能です。リッチテキスト機能を参照してください。
var richText = new Text("", Font.GetDefault(16));richText.UseRichText = true;
// PTMLタグを使用richText.Content = """ <color=red>赤い文字</color> と <color=blue>青い文字</color> <size=24>大きな文字</size> と <size=12>小さな文字</size> <b>太字</b> と <i>斜体</i> """;