Skip to main content
Known Participant
October 7, 2010
Answered

click on color button and write in textarea with that color font

  • October 7, 2010
  • 1 reply
  • 2626 views

hi everyone

I want to do as follows:

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

Isn't this possible in flash 10 using AS3 ??????

I tried using setTextFormat but the problem is i have to have text before inserting format on that.

Please somebody tell me how to do this?????

Thanks alot in advance

Regards

This topic has been closed for replies.
Correct answer kglad

Right now what i have done is  i appended a space at the end of text and using setTextFormat i set its  color. then it works like i want. BUT still there are 2 problems

  • space is always included which user has to MANUALLY REMOVE
  • i can't added character inbetween i can only append at the end of text

If i remove space character then format is reset to original only..... so can't even remove that character programmatically.

Just to let u know..

Thanks alot

Regards


:

import fl.controls.Button;
import fl.controls.TextArea;
import flash.text.TextField;
import flash.events.MouseEvent;


var tf2:TextField = createTextField(100,100,200,22,true);

tf2.type = 'input';
tf2.background = true;
tf2.backgroundColor = 0x777777;
tf2.width = 400;
tf2.height = 200;
tf2.x = 100;
tf2.y = 100;

var df1:TextFormat = new TextFormat();
df1.bold = false;
//df1.align = "center";
df1.color = 0xffffff;
df1.size = 14;
tf2.defaultTextFormat = df1;
tf2.text = "[text]";
//tf2.setTextFormat(df1, 0);

//addChild(ta2);
//ta2.move(150,100);

tf2.addEventListener(Event.CHANGE,f);

function f(e:Event):void{
    tf2.setTextFormat(df1,tf2.caretIndex-1);
}

var btn_Colour_Red:Button = new Button();
btn_Colour_Red.label = "Red";
btn_Colour_Red.addEventListener(MouseEvent.CLICK, btn_Colour_Red_Clicked);
btn_Colour_Red.setSize(40, 20);
btn_Colour_Red.move(350, 0);
addChild(btn_Colour_Red);

var btn_Colour_Blue:Button = new Button();
btn_Colour_Blue.label = "Blue";
btn_Colour_Blue.addEventListener(MouseEvent.CLICK, btn_Colour_Blue_Clicked);
btn_Colour_Blue.setSize(40, 20);
btn_Colour_Blue.move(250, 0);
addChild(btn_Colour_Blue);


function btn_Colour_Red_Clicked(e:MouseEvent) {
   
    df1.bold = false;
    df1.align = "center";
    df1.color = 0xFF0000;
    df1.size = 14;
    //ta2.setFocus();
    stage.focus = tf2;
}

function btn_Colour_Blue_Clicked(e:MouseEvent) {
   
    df1.bold = false;
    df1.align = "center";
    df1.color = 0x0000FF;
    df1.size = 14;
    //ta2.setFocus();
    stage.focus = tf2
}

function createTextField(x:Number, y:Number, width:Number, height:Number, border:Boolean):TextField {
    var result:TextField = new TextField();
    result.x = x;
    result.y = y;
    result.width = width;
    result.height = height;
    result.background = true;
    result.border = border;
    addChild(result);
    return result;
}

1 reply

kglad
Community Expert
Community Expert
October 7, 2010

use the defaultTextFormat property of your textfield to assign a format for text that will be entered into your textfield.

Known Participant
October 7, 2010

hi thanks for replying...

I already tried to do that but the problem is in textarea whatever format i apply to its textfield but i won't show up. so i have to use htmlText instead to apply colors.

Am i doing some mistake somewhere?

Regards

kglad
Community Expert
Community Expert
October 7, 2010

yes, you're made an error somewhere.  but if using htmlText is working for you, use it.