▼ActionScript 2.0メモ▼
文字列の表示


文字列表示を行うFlashを作成する。



書式
表示する文字列の書式はTextFormatクラスで指定する。TextFormatクラスの主なプロパティは次の通り。

font フォント名
size フォントサイズ
color 文字色
bold ボールド(true/false)
italic イタリック(true/false)
underline アンダーライン(true/false)


フォントにはローカルPCにインストールされているフォント「デバイスフォント」と、Flashコンテンツに埋め込んだフォント「埋め込みフォント」がある。「デバイスフォント」を使う時は、TextFormatクラスのfontプロパティに「実際のフォント名」もしくは次の引数を指定する。
_serif セリフつき欧文フォント
_sans セリフなし欧文フォント
_typewriter 等幅欧文フォント
_明朝 セリフつき日本語フォント
_ゴシック セリフなし日本語フォント
_等幅 等幅日本語フォント
フォントサイズはドット単位でなくポイント単位なので注意すること。


ソースコードの文字コード
日本語を表示する時は、ソースコードの文字エンコードを「UTF-8」にする。


サンプルプログラムのラベル
以下のサンプルプログラムでは、TextFieldオブジェクトに次のメソッドを追加している。
また、インスタンス名は自動生成している。
setXY(x:Number,y:Number) XY座標の指定
setTest(text:String) テキストの指定
setFontSize(size:Number) フォントサイズの指定
setFontColor(color:Number) フォント色の指定


ソースコード
LabelEx.as
//文字列の表示
class LabelEx {

    //コンストラクタ
    function LabelEx(mc:MovieClip) {
        //HelloWorld
        addLabel(mc,"Hello World!");

        //24ドットの赤い文字を(0,20)に描画
        var test1:TextField=addLabel(mc,"1234567890");
        test1.setXY(0,20);
        test1.setFontColor(0xFF0000);
        test1.setFontSize(24);
    }

    //ラベルの追加
    static function addLabel(parent:MovieClip,
        text:String):TextField {
        //初期値
        if (text==undefined) text="";

        //インスタンス名の自動生成
        if (parent.nameIdx==undefined) parent.nameIdx=0;
        var name:String="_name"+(parent.nameIdx++);

        //テキストフィールドの追加
        parent.createTextField(name,
            parent.getNextHighestDepth(),0,0,0,0);
        var mc:TextField=parent[name];
        mc.autoSize  ="left";//オートサイズ
        mc.selectable=false; //選択不可
        mc.text=text;        //テキスト
        mc._x  =0;           //X座標
        mc._y  =0;           //Y座標
        
        //書式
        mc.format=new TextFormat();
        mc.format.font ="_等幅"; //フォント
        mc.format.size =12;      //文字サイズ
        mc.format.color=0x000000;//文字色
        mc.setTextFormat(mc.format);
        
        //XY座標の指定
        mc.setXY=function(_x:Number,_y:Number) {
            this._x=_x;
            this._y=_y;
        }

        //テキストの指定
        mc.setText=function(_text:String) {
            this.text=_text;
            this.setTextFormat(this.format);
        }
        
        //文字サイズの指定
        mc.setFontSize=function(size:Number) {
            this.format.size=size; 
            this.setTextFormat(this.format);
        }
        
        //文字色の指定
        mc.setFontColor=function(color:Number) {
            this.format.color=color;
            this.setTextFormat(this.format);
        }
        return mc;
    }

    //メイン
    static function main() {
        var app:LabelEx=new LabelEx(_root);
    }
}

コンパイル
mtasc -swf LabelEx.swf -main LabelEx.as -version 7 -header 240:240:10



−戻る−