单击颜色按钮并使用该颜色字体在textarea中写入

时间:2010-10-07 02:18:37

标签: actionscript-3 textarea flash-10

我想做如下:

* 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)

请有人告诉我该怎么做?

2 个答案:

答案 0 :(得分: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函数。

答案 1 :(得分:0)

以下是任何正在环顾四周的人的解决方案(感谢那些解决了我的问题的人)

在textfield的changehandler中使用以下代码:

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

在按钮的点击处理程序中使用以下内容:

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

此致