I have a string that is too long to fit into my 100px container. I want to make TextField automatically multiline if needed. How do I make it?
I think what you're looking for is TextFieldAutoSize.
var tf:TextField = new TextField();
tf.multiline = true;
tf.wordWrap = true;
tf.autoSize = TextFieldAutoSize.LEFT;
tf.text = yourLongStringVar;
For more information check out the documentation.
EDIT : added wordwrap and multiline properties to maintain width
Have you tried using TextField.multiline or .wordWrap?
var tf:TextField = new TextField();
tf.multiline = true;
tf.wordWrap = true;
// etc..
Related
So I'm trying to use align to centralize the text in my textfield
var inputFieldOne:TextField= new TextField();
inputFieldOne.width= 150;
inputFieldOne.height= 50;
inputFieldOne.x= 180;
inputFieldOne.border= true;
inputFieldOne.type= "input";
inputFieldOne.restrict= "0-9";
inputFieldOne.maxChars= 1;
inputFieldOne.background= true;
inputFieldOne.backgroundColor= 0x00FF00;
inputFieldOne.align = TextFormatAlign.CENTER;
inputFieldOne.defaultTextFormat = new TextFormat("verdana", 45);
and I get this error:
1119: Access of possibly undefined property align through a reference with static type flash.text:TextField.
help?
TextField does not have an align property. You may be confusing it with TextFormat which does have the property.
Try the following:
var textFormat:TextFormat = new TextFormat("verdana", 45);
textFormat.align = TextFormatAlign.CENTER:
var inputFieldOne:TextField= new TextField();
inputFieldOne.width= 150;
inputFieldOne.height= 50;
inputFieldOne.x= 180;
inputFieldOne.border= true;
inputFieldOne.type= "input";
inputFieldOne.restrict= "0-9";
inputFieldOne.maxChars= 1;
inputFieldOne.background= true;
inputFieldOne.backgroundColor= 0x00FF00;
inputFieldOne.defaultTextFormat = textFormat;
My company has a Apache Flex 4.11 application. In this application we use Text Flow to allow our users to create text boxes on a page and edit the text. The problem we are having is that when a user types in a word that is longer then the textbox is wide the word will be broken and the remainder of the word will be put on a new line. What we need is for the word to rather be truncated or allowed to go past the X bound of the text box. Is this possible?
var myFormat:TextFormat = new TextFormat();
myFormat.size = 15;
var myText:TextField = new TextField();
myText.defaultTextFormat = myFormat;
myText.text = "HEADLINE";
myText.border = true;
myText.wordWrap = true;
myText.width = 25;
myText.height = 200;
myText.x = 10;
myText.y = 10;
addChild(myText);
If you need more information I will be happy to provide it.
I have a bitmap data which draw a text field. after scaling, text are distortion.
I using following code:
// tf is text Field and bm is bitmap.
var tf:TextField = new TextField();
tf.text = "Hello world";
var bd:BitmapData = new BitmapData(200, 200, true, 0x00ff00);
bd.draw(tf);
var bm:Bitmap = new Bitmap(bd);
addChild(bm);
bm.scaleX = 2;
bm.scaleY = 2;
Please guide me.
You should use transform matrix to draw an upscaled text field (or any other vector graphics object) onto a BitmapData.
var mat:Matrix=new Matrix();
mat.scale(2.0,2.0);
bd.draw(tf,mat);
First, increase the font size and then convert and scale as bitmap like so,
//---- Text format ----
var textFormat:TextFormat = new TextFormat();
textFormat.font = "Arial";
//textFormat.bold = true;
textFormat.size = 40;
//---------------------------------------------
//
var tf:TextField = new TextField();
tf.text = "Hello world";
/*tf.antiAliasType = AntiAliasType.ADVANCED;
tf.gridFitType = GridFitType.PIXEL;
tf.thickness = 0;
tf.sharpness = 0;*/
tf.setTextFormat(textFormat);
//
//
var bd:BitmapData = new BitmapData(200,200,true,0x00ff00);
bd.draw(tf);
var bm:Bitmap = new Bitmap(bd);
bm.smoothing = true;
addChild(bm);
bm.scaleX = 2;
bm.scaleY = 2;
Best luck.
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;
I am trying to get some words from xml and put them an the stage side by side in the center of the stage. I achieved this by the code below. I auto resize textfield according to text inside. But this time there comes space between words. What I accomplish is to have autoresized and adjacent words without space between them. But I could not solve the problem.
Could you please help me to solve it.
Thanks in advance
var partsWidth=100;
var wordTf = new TextField();
wordTf.name =thispart;
wordTf.text =thispart;
wordTf.width=partsWidth;
xStartPoint=stage.stageWidth / 2 - (numberOfWords * partsWidth )/2;
wordTf.height=partsHeight;
wordTf.x= xStartPoint + (index * partsWidth) ;
wordTf.y=150;
wordTf.background=true;
wordTf.backgroundColor = 0xe3e3e3;
wordTf.border = true;
var myFormat:TextFormat = new TextFormat();
myFormat.size = 16;
myFormat.align = TextFormatAlign.CENTER;
wordTf.setTextFormat(myFormat);
wordTf.autoSize=TextFieldAutoSize.CENTER;
addChild(wordTf);
you are setting the width explicit with wordTf.width=partsWidth;. this will override the autosize option. I would use the following code.
var container:Sprite = new Sprite();
var myFormat:TextFormat = new TextFormat();
myFormat.size = 16;
myFormat.align = TextFormatAlign.CENTER;
for each( var thispart:String in parts )
{
var wordTf = new TextField();
wordTf.defaultTextFormat = myFormat;
wordTf.name = thispart;
wordTf.text = thispart;
wordTf.height=partsHeight;
wordTf.background=true;
wordTf.backgroundColor = 0xe3e3e3;
wordTf.border = true;
wordTf.width = wordTf.textWidth + 4;
wordTf.y=150;
wordTf.x = container.width;
container.addChild(wordTf);
}
container.x = (stage.stageWidth - container.width) / 2;
addChild(container);
add your words to a separate sprite, and after all words added, add this sprite to the stage and center it.
The line
wordTf.width = wordTf.textWidth + 4;
is the important one. After setting the text, flash can calculate the width of the text. now set this text width (+4 is a fixed padding around the text in a text field you can't modify) as width of your textfield.