コメントの表示 | RSS フィード

getTextExtent (TextFormat.getTextExtent メソッド)

public getTextExtent(text:String, [width:Number]) : Object

Flash Player 8 以降では使用しないでください。これに代わるものはありません。

my_fmt に指定したフォーマットのテキストストリング text のテキスト寸法情報を返します。text ストリングは標準テキスト (非 HTML) として扱われます。

このメソッドは、6 つのプロパティを持つオブジェクトを返します。ascent、descent、width、height、textFieldHeight、およびtextFieldWidthがあります。すべてのプロパティ値はピクセル単位です。

width パラメータを指定した場合は、指定したテキストに折り返しが適用されます。これにより、指定したすべてのテキストを表示するために必要なテキストボックスの高さを判断することができます。

ascent プロパティはテキスト行のベースラインよりも上の長さ、descent プロパティはテキスト行のベースラインよりも下の長さを表します。最初のテキスト行のベースラインは、テキストフィールドの基準点に ascent パラメータ値を加算した位置になります。

width プロパティと height プロパティは、テキストストリングの幅と高さを示します。textFieldHeight プロパティと textFieldWidth プロパティは、テキストストリング全体を表示するのに必要なテキストフィールドオブジェクトの高さと幅を表します。テキストフィールドの周囲には 2 ピクセル幅の "ふち取り" があるので、textFieldHeight の値は height + 4 になります。同様に、textFieldWidth の値は常に width + 4 になります。

テキストメトリクスに基づいてテキストフィールドを作成する場合は、height ではなく textFieldHeight を、width ではなく textFieldWidth を使用してください。

次の図に、この寸法を示します。



TextFormat オブジェクトをセットアップする際には、フォント名、フォントサイズ、行間を含め、テキストフィールドを作成する際とまったく同じようにすべての属性を設定する必要があります。行間のデフォルト値は 2 です。

使用できるバージョン : ActionScript 1.0、Flash Player 6

パラメータ

text:String - ストリング。

width:Number (オプション) - 指定したテキストを折り返す幅を示す数値 (ピクセル単位)。

戻り値

Object - width、height、ascent、descent、textFieldHeight、textFieldWidth の各プロパティを持つオブジェクト。

次の例では、指定したフォーマットを使ったテキストストリングを表示する、ストリングと同じサイズの単一行テキストフィールドを作成します。

var my_str:String = "Small string";

// Create a TextFormat object,
// and apply its properties.
var my_fmt:TextFormat = new TextFormat();
with (my_fmt) {
    font = "Arial";
    bold = true;
}

// Obtain metrics information for the text string
// with the specified formatting.
var metrics:Object = my_fmt.getTextExtent(my_str);

// Create a text field just large enough to display the text.
this.createTextField("my_txt", this.getNextHighestDepth(), 100, 100, metrics.textFieldWidth, 
metrics.textFieldHeight);
my_txt.border = true;
my_txt.wordWrap = true;
// Assign the same text string and TextFormat object to the my_txt object.
my_txt.text = my_str;
my_txt.setTextFormat(my_fmt);

次の例では、指定したフォーマットのテキストストリングを表示できるだけの高さで、複数行の 100 ピクセル幅のテキストフィールドを作成します。

// Create a TextFormat object.
var my_fmt:TextFormat = new TextFormat();
// Specify formatting properties for the TextFormat object:
my_fmt.font = "Arial";
my_fmt.bold = true;
my_fmt.leading = 4;

// The string of text to be displayed
var textToDisplay:String = "Macromedia Flash Player 7, now with improved text metrics.";

// Obtain text measurement information for the string,
// wrapped at 100 pixels.
var metrics:Object = my_fmt.getTextExtent(textToDisplay, 100);

// Create a new TextField object using the metric
// information just obtained.
this.createTextField("my_txt", this.getNextHighestDepth(), 50, 50-metrics.ascent, 100, 
metrics.textFieldHeight);
my_txt.wordWrap = true;
my_txt.border = true;
// Assign the text and the TextFormat object to the TextObject:
my_txt.text = textToDisplay;
my_txt.setTextFormat(my_fmt);

Comments


Fumio Nonaka が Jan 21, 2006 の 12:45 AM に追加:
[誤]
「Flash Player 8以降では使用しないでください。これに代わるものはあ
りません。」

[正]
*削除(Flash Player 8でもサポートされます)

[誤]
「使用できるバージョン : ActionScript 1.0、Flash Player 6」

[正]
「使用できるバージョン : ActionScript 1.0、Flash Player 7」

*英語版LiveDocs「getTextExtent (TextFormat.getTextExtent method)」
のComments参照。

 

RSS フィード | このページに新しいコメントが追加された場合に、電子メールでの通知を希望する。 | コメントレポート

現在のページ: http://livedocs.adobe.com/flash/8_jp/main/00002807.html