actionscript 3 - click on color button and write in textarea with that color font -


i want follows:

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

isn't possible in flash 10 using as3 ??????

i tried using settextformat problem have have text before inserting format on that.

this want:

* start writing in textarea white color (eg: "abc") * click blue color button * start writing in textarea blue color (eg: "abcdef" - abc in white , def in blue) * click red color button * start writing in textarea red color (eg: "abcdefxyz" - abc in white , def in blue , xyz in red) 

please tell me how this?

the documentation states if want change format of text prior writing in text field assign new defaulttextformat. otherwise, setting new format change current selection.

the solution below works maintaining focus on text field, when buttons clicked text field still has focus. if there current selection, selection change either blue or red, depending on button pressed. if there no selection, new defaulttextformat applied, without changing previous defaulttextformats, since text field still had focus when new format applied.

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 clicked private function fieldfocusouthandler(evt:focusevent):void     {     stage.focus = evt.currenttarget 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);             }     } } } 

alternatively, if have other buttons in program not relate text field , should make text field lose focus when clicked, remove fieldfocusouthandler function , place stage.focus = field; inside buttonclickhandler method. keep , tailor fieldfocusouthandler function if issue.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -