簡體   English   中英

單擊顏色按鈕並使用該顏色字體在textarea中寫入

[英]click on color button and write in textarea with that color font

我想做如下:

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

請有人告訴我該怎么做?

文檔指出,如果要在文本字段中寫入任何內容之前更改文本格式,以分配新的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函數。

這是環顧四周的人的解決方案(感謝那些解決了我的問題的人)

在textfield的changehandler中使用以下命令:

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

在按鈕的點擊處理程序中使用以下命令:

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

問候

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM