save text of textarea and reuse it to replace text in textarea - actionscript-3

Is it possible to save text of textarea (flash 10, as3, cs5) in some variable or so and with its textformat (more than one color) and then reuse it to replace text in textarea?
I tried saving htmlText of textarea but the problem is when i replace it in textarea tags causes problem. There will always be another extra line.
If anyone wants to view p tags problem try following. Just click on text and then move your down arrow key, cursor will go to next line.
import fl.controls.TextArea;
var txtHTML:TextArea = new TextArea();
txtHTML.move(0,0);
var default_format:TextFormat = new TextFormat();
default_format.font = "Arial";
default_format.bold = false;
default_format.align = "center";
default_format.color = 0xFFFF00;
default_format.size = 14;
var field:TextField = txtHTML.textField;
field.defaultTextFormat = default_format;
field.setTextFormat(default_format);
field.alwaysShowSelection = true;
field.background = true;
field.type = 'input';
field.multiline = true;
field.backgroundColor = 0x777777;
field.embedFonts = true;
txtHTML.htmlText = '<P ALIGN="CENTER"><FONT FACE="_sans" SIZE="14" COLOR="#FFFF00" LETTERSPACING="0" KERNING="0">ASDF</FONT></P>';
field.x = 0;
field.y = 0;
field.width = 400;
field.height = 200;
field.text = "";
addChild(txtHTML);
Is there a way to do this?

Just copy the text and remove the last character i.e. '>'. So there won't be <p></p> tags problem of extra line.
Regards

Related

How do I format different lines of text differently, from a textfield created in AS3, with text populated from a txt file?

So I have a flash file in AS3, latest version of flash.
It creates a text box in AS3. It then uses AS3 to grab text from a text file (2 lines) and loads it in. I then used further code to format the text size, font, color etc.
But NOW...I need line 1 of the text box to be a certain format (large, caps) and the second line to be a different format (smaller, no caps)
Here is all my code below:
//BEGIN TXT LOADER
var myTextLoader:URLLoader = new URLLoader();
var winnerText:TextField = new TextField();
myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
function onLoaded(e:Event):void {
winnerText.text = e.target.data;
addChild(winnerText);
}
myTextLoader.load(new URLRequest("EditableText.txt"));
//BEGIN TEXT BOX FORMATTING
winnerText.width = 1920;
winnerText.height = 300;
winnerText.y = 430;
//BEGIN TEXT & FONT FORMATTING
var casinoBranding:TextFormat = new TextFormat();
casinoBranding.size = 90;
casinoBranding.align = TextFormatAlign.CENTER;
casinoBranding.font = "Bliss Pro";
casinoBranding.leading = -50;
winnerText.defaultTextFormat = casinoBranding;
You can apply a TextFormat like #Aaron suggests. Another way of doing is to use stylesheets. Here is an example
http://snipplr.com/view/39474/as3-textfield-and-stylesheet-example-created-in-actionscript/
You can apply a TextFormat to a specific range of text using TextField/setTextFormat().
To apply a different text format to the first line of text you can do this:
var casinoBranding:TextFormat = new TextFormat();
var casinoBrandingFirstLine:TextFormat = new TextFormat();
// ... apply formatting options
function onLoaded(e:Event):void {
winnerText.defaultTextFormat = casinoBranding;
winnerText.text = e.target.data;
winnerText.setTextFormat(casinoBrandingFirstLine, 0, winnerText.getLineOffset(1));
}
Note that if word wrapping is involved it changes what the "first line" really means.

DataGrid header text alignment in as3

I want to change datagrid header text alignment from left to center.
this is what is used
// sample code
var tf:TextFormat = new TextFormat();
tf.size = 15;
tf.font = "Arial" ;
tf.color = 0x0000FF;
tf.align = "CENTER";
// also tried tf.align = TextFormatAlign.CENTER;
var room_name:DataGridColumn = new DataGridColumn("Room_Name");
room_name.headerText = "Room Name";
var minimum_chips:DataGridColumn = new DataGridColumn("Minimum_chips");
minimum_chips.headerText = "Minimum Chips";
dtgrid.setRendererStyle("textFormat", tf);
dtgrid.setStyle("headerTextFormat",tf);
dtgrid.columns = [room_name , minimum_chips];
changing any values for text format reflects in header text expect the alignment . I want to set the alignment center. Thanks.
How about change css?
DataGrid
{
headerStyleName:dataGridHeaderStyle;
}
.dataGridHeaderStyle
{
textAlign:center;
fontWeight:bold;
}
Or if you want to define style at as3, write following code.
var newstyle:CSSStyleDeclaration = new CSSStyleDeclaration();
newstyle.setStyle("textAlign", "center");
newstyle.setStyle("fontWeight", "bold");
dtgrid.setStyle("headerStyleName", newstyle);

Text becomes invisible when i update textfield.text

I have tried googling for so long now, but I cannot find the solution for this problem.
I have a textfield, with 3 strings and 3 variables. When the variables change I want the textField to display the updated version of the variables, so I use textfield.text = "newly updated text" but then the text is not visible. After the update event triggers, the text "disappears", but it's still there If I trace(textField.text) it outputs the right info. I have embedded the text aswell.
Any help is deeply appreciated
public var infoBox:TextField = new TextField ;
public var myFormat:TextFormat = new TextFormat;
myFormat.color = 0xAA0000;
myFormat.size = 30;
myFormat.font = "Font1";
myFormat.italic = false;
myFormat.align = TextFormatAlign.LEFT;
infoBox.text = "BonusbonusDamage: " + bonusDamage + "\nFlame: " + flame + "\nSticky" + sticky;
infoBox.type = "dynamic";
infoBox.width = 300;
infoBox.height = 150;
infoBox.border = true;
infoBox.borderColor = 0xFFFFFF;
infoBox.background = true;
infoBox.backgroundColor = 0x000000;
infoBox.wordWrap = true;
infoBox.setTextFormat(myFormat);
infoBox.x = 25;
infoBox.y = 20;
addChild(infoBox);
public function updateText()
{
infoBox.text = "BonusbonusDamage: " + bonusDamage + "\nFlame: " + flame + "\nSticky" + sticky;
This is where the text becomes invisible, even thoug its .visible is true and .alpha Is 100.
}
Try infobox.embedFonts = true; as your Font1 is apparently an embedded font. This is a pretty known issue with custom fonts in a text field.
TextField.embedFonts manual
EDIT: Yep, you are using setTextFormat() and this format is cleared whenever you replace text property. Use defaultTextFormat property instead.
infoBox.defaultTextFormat=myFormat;

AS3 Why does editing a Textfield inside of a MovieClip throw all the sizes off?

I'm pretty frustrated with AS3 right now. It seems like there must be serious issues with how things scale inside of movieclips. From the stage everything seems to work fine.
On one of my projects I try to set a textfield.width equal to its container(movieclip)
tf.width = mc.width; Of course it says everything is even but when I look at the border of the textfield it's nowhere near the size of the movie clip it's contained within.
While trying to make a much smaller sample to share with you guys for help, that part worked, but trying to resize text did something completely different. Can anyone help me make sense of all this? The code below seemingly randomly starts changing the size of everything.
Also, I was trying to follow this code from here but just kept getting an error when trying to change the format size in a while loop: Autosize text to fit the width of a button
var square:MovieClip = new MovieClip();
addChild(square);
square.graphics.lineStyle(3,0x00ff00);
square.graphics.beginFill(0x0000FF);
square.graphics.drawRect(0,0,100,100);
square.graphics.endFill();
square.x = 0;
square.y = 0;
square.width = 200;
square.height = 200;
var tffSize = 400;
var tffOr:TextFormat = new TextFormat();
tffOr.size = tffSize;
tffOr.align = TextFormatAlign.CENTER;
var tf:TextField = new TextField();
square.addChild(tf);
tf.defaultTextFormat = tffOr;
tf.text = "Hello";
tf.border = true;
tf.width = square.width;
tf.height = square.height;
trace(tf.textWidth);
trace(square.width);
while (tf.textWidth > square.width || tf.textHeight > square.height)
{
trace("too Big");
newTFF();
trace(tf.textWidth + " vs " + square.width);
square.width = 200;
trace(tf.textWidth + " vs " + square.width);
}
function newTFF()
{
tffSize = tffSize - 1;
var tff:TextFormat = new TextFormat();
tff.size = tffSize;
tff.align = TextFormatAlign.CENTER;
//tf.defaultTextFormat = tff;
tf.setTextFormat(tff);
tf.autoSize = "left";
}

Change font-color of TextField inside MovieClip

I've a problem to change the font-color of a TextField after it has been added as a child to a MovieClip.
My code:
var btn:MovieClip = new MovieClip();
// other code for button
var tf:TextFormat = new TextFormat();
tf.size = 12;
tf.bold = true;
tf.font = "Arial"
tf.color = 0x000000;
var capt:TextField = new TextField();
capt.defaultTextFormat = tf;
capt.width = 200;
capt.height = 50;
capt.text = "Test";
capt.y = 20;
capt.x = 20;
capt.border = false;
capt.selectable = false;
btn.addChild(capt);
// .....
How can I Change the font-color after the last line?
It sounds like you're looking for TextField.setTextFormat(). You can either adjust your original TextFormat or just make a whole new one.
tf.color = 0xFF0000;
capt.setTextFormat(tf);
Assuming the TextField falls out of scope after that last line (you don't show enough to know if it does or not), you'll need to loop through the button to get the TextField and do it from there.
var i:uint;
var l:uint = btn.numChildren; //numChildren and length are 'heavy' getters, never use them as restrictors in a loop
for ( i = 0; i < l; i++ ) {
var cur:DisplayObject = btn.getChildAt( i );
if ( cur is TextField ) {
( cur as TextField ).setTextFormat( /*set new TextFormat in here*/);
break;
}
}
That assumes there is only the single TextField, of course. If there are multiple, I would extend MovieClip and add a public property for the value you want to change.
You can also just use the textColor property of a textfield:
capt.textColor = 0xFF0000;