1

我想做如下:

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

这在使用 AS3 的 flash 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。否则,设置新格式将更改当前选择。

下面的解决方案通过保持对文本字段的焦点来工作,因此当单击按钮时,文本字段仍然具有焦点。如果有当前选择,则选择将更改为蓝色或红色,具体取决于按下的按钮。如果没有选择,则应用新的 defaultTextFormat,而不更改以前的 defaultTextFormats,因为在应用新格式时文本字段仍然具有焦点。

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 回答