1

私は次のようにしたい:

* click a "red" button
* write in textarea with red color font
* click "blue" button
* write in textarea with blue color font

これは、AS3 を使用するフラッシュ 10 で可能ではないでしょうか??????

setTextFormat を使用してみましたが、問題は、フォーマットを挿入する前にテキストが必要なことです。

これは私が欲しいものです:

* start writing in textarea with WHITE COLOR (eg: "abc")
* click BLUE COLOR BUTTON
* start writing in textarea with BLUE COLOR (eg: "abcdef" - abc in white and def in blue)
* click RED COLOR BUTTON
* start writing in textarea with RED COLOR (eg: "abcdefxyz" - abc in white and def in blue and xyz in red)

誰かがこれを行う方法を教えてください。

4

2 に答える 2

1

ドキュメントには、テキスト フィールドに何かを書き込む前にテキストの形式を変更する場合は、新しい defaultTextFormat を割り当てると記載されています。それ以外の場合、新しい形式を設定すると、現在の選択が変更されます。

以下のソリューションは、テキスト フィールドにフォーカスを維持することで機能するため、ボタンをクリックしてもテキスト フィールドにフォーカスがあります。現在の選択がある場合、押されたボタンに応じて、選択が青または赤に変わります。選択がない場合は、新しいフォーマットが適用されたときにテキスト フィールドにまだフォーカスがあるため、以前の defaultTextFormats を変更せずに、新しい defaultTextFormat が適用されます。

package
{   
import flash.display.Sprite;
import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.TextFormat;
import flash.events.MouseEvent;
import flash.events.FocusEvent;

public class ChangeTextColor extends Sprite
{
private var field:TextField;
private var redButton:Sprite;
private var blueButton:Sprite;

public function ChangeTextColor()
    {
    init();
    }

//Initialize
private function init():void
    {
    //Create Text Field
    field = new TextField();
    field.type = TextFieldType.INPUT;
    field.border = true;
    field.x = field.y = 10;
    addChild(field);

    //Retain Focus On TextField
    field.addEventListener(FocusEvent.FOCUS_OUT, fieldFocusOutHandler);

    //Create Button
    redButton = createButton(10, 120, 200, 20, 0xFF0000);
    blueButton = createButton(10, 150, 200, 20, 0x0000FF);
    }

//Create Button Method
private function createButton(x:uint, y:uint, width:uint, height:uint, color:uint):Sprite
    {
    var resultSprite:Sprite = new Sprite();

    resultSprite.graphics.beginFill(color);
    resultSprite.graphics.drawRect(0, 0, width, height);
    resultSprite.graphics.endFill();

    resultSprite.addEventListener(MouseEvent.CLICK, mouseClickEventHandler);

    resultSprite.x = x;
    resultSprite.y = y;
    addChild(resultSprite);

    return resultSprite;
    }

//Apply Text Format
private function changeTextFormatColor(color:uint):void
    {
    var format:TextFormat = new TextFormat();
    format.color = color;

    //Change Format Of Selection Or Set Default Format
    if  (field.selectionBeginIndex != field.selectionEndIndex)
        field.setTextFormat(format, field.selectionBeginIndex, field.selectionEndIndex);
        else
        field.defaultTextFormat = format;
    }

//Maintain Focus Of TextField When Color buttons Are Clicked
private function fieldFocusOutHandler(evt:FocusEvent):void
    {
    stage.focus = evt.currentTarget as TextField;
    }

//Button Click Event Handler
private function mouseClickEventHandler(evt:MouseEvent):void
    {
    switch  (evt.currentTarget)
            {
            case redButton:     trace("red clicked");
                                changeTextFormatColor(0xFF0000);
                                break;

            case blueButton:    trace("blue clicked");
                                changeTextFormatColor(0x0000FF);
            }
    }
}
}

または、テキスト フィールドに関連しない他のボタンがプログラムにあり、クリックするとテキスト フィールドのフォーカスが失われる場合は、fieldFocusOutHandler 関数を削除して、stage.focus = field; を配置します。buttonClickHandler メソッド内。これが問題になる場合は、fieldFocusOutHandler 関数を保持して調整することもできます。

于 2010-10-07T04:45:03.280 に答える
0

これが周りを見ている人のための解決策です(私の問題を解決してくれた人に感謝します)

テキストフィールドの変更ハンドラで次を使用します。

textfield.setTextFormat(default_format, textfield.caretIndex-1, textfield.caretIndex);

ボタンのクリックハンドラで次を使用します。

default_format.color = getColor("red");
    stage.focus = textfield;

よろしく

于 2010-10-10T22:12:13.023 に答える