2010-10-07 95 views
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) 

請有人告訴我如何做到這一點?

回答

0

這裏是人誰是尋找周圍的文本框的changehandler以下(感謝那些誰解決了我的問題)

使用該解決方案:

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

使用按鈕的函數clickhandler如下:

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

Regards

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 =領域;在buttonClickHandler方法中。如果這是一個問題,您也可以保留並調整fieldFocusOutHandler函數。

+0

嗨,感謝您的回覆,但這不是我真的很想。我不想改變整個textarea的fontcolor。 要在textarea的部分設置不同的字體,我必須使用setTextFormat(objTextFormat,beginIndex,endIndex)。但在寫之前,我沒有beginIndex和endIndex,所以我不能這樣做。我已經編輯了我的問題,請檢查一下。問候 – user427969 2010-10-07 22:46:29

+0

我編輯了我的答案,顯示工作解決方案 – TheDarkIn1978 2010-10-08 01:46:53

+0

您好,非常感謝,例如。一個小問題是,如果我們選擇紅色並開始在藍色文本之間打字,那麼寫入的文本將是藍色而不是紅色。我發現了另一種方法。非常感謝你的幫助。問候 – user427969 2010-10-10 22:11:16