ComboBox items Styling - actionscript-3

I have a ComboBox that contains the names of some fonts, if the user chooses an item (a font), the text inside a text field changes to the corresponding font.
I just want to display each font in the item that holds it, similar to microsoft word:
https://i.stack.imgur.com/qGRsz.png
Not what i have here, the same font for all of them:
https://image.ibb.co/ca55qR/combobox.png
I tried the following:
var tf:TextFormat = new TextFormat();
tf.font = "Tahoma";
tf.size = 12;
myFontsBox.textField.setStyle("textFormat", tf);
the "Tahoma" font is applied to every item in the ComboBox, the desired result is for it to be applied only on the "Tahoma" item.
Is there any way i can change the styling of the items inside the ComboBox ??

You could try something like this:
var dropdown:List = myFontsBox.dropdown;
for (var i:int = 0; i < dropdown.length; i++)
{
var item = dropdown.getItemAt(i);
var tf:TextFormat = new TextFormat();
tf.font = dropdown.itemToLabel(item);
tf.size = 12;
item.setStyle("textFormat", tf);
}

Related

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);

Flex TLF Word Wrap Breaks Words

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.

Text Field in Actionscript3

I am trying to create a text field manually from actionscript alone. I got parts of it but now i don't know how to give the fonts size and different fonts can somebody tell me how to go about this?
for (var i:Number = 0; i < weekdaystringArray.length ; i++)
{
weekdaystring.text = weekdaystringArray [i];
weekdaystring.x = 150;
weekdaystring.y = 994;
addChild (weekdaystring);
weekdaystring.textColor = 0xFFFFFF;
}
This is what I have so far...
You need to apply a TextFormat to the TextField with the setTextFormat() method.
var format:TextFormat = new TextFormat();
format.font = "Courier";
format.size = 24;
// then inside your loop:
weekdaystring.setTextFormat(format);
it need to apply a TextFormat to the TextFiled
var format:TextFormat = weekdaystring.defaultTextFormat;
format.font = "courier";
format.size = 24
weekdaystring.defaultTextFormat = format;

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;

actionscrip3 textfield sizeing end centering

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.