Change the width of a character to match the size of a TextField - actionscript-3

I've read tons of posts about setting the size of a TextField to match the width of the string containing it. I'm looking for a way to do it the other way around. The TextField in my application should have a fixed with and I want to squeeze the text in it by changing the width of a single character.
The solution I'm working width now decreases the size of the font until it fits into the TextField, but I want to squeeze the text (so decreasing just width, not height) until it fits in the TextField. Any ideas how to achieve this?

I don't think it's possible to scale font on a single axis.
The only thing i could think of is to bake the text field and scale the resulting bitmap:
var tf:TextField = new TextField();
tf.text = "bitmap text";
var myBitmapData:BitmapData = new BitmapData(80, 20);
myBitmapData.draw(tf);
var bmp:Bitmap = new Bitmap(myBitmapData);
this.addChild(bmp);
Source

Related

how to resize dynamic text textfield to responsive if stage size change? AS3

I'm working on a puzzle game using text to arrange, for the blocks themselves I create a children DisplayObject which contains a Texfield and I set the size of the texfield using a variable because each frame has a different font size, for example:
var sizeOfTF = 35;
var my_text_format: TextFormat = new TextFormat();
my_text_format.size = sizeOfTF ; // set size
puzzle.text.defaultTextFormat = my_text_format;
puzzle.text.setTextFormat(my_text_format);
puzzle.text.autoSize = TextFieldAutoSize.LEFT;
stage.addChild(DisplayObject(puzzle));
then I have a project with a stage size of 720x1280 and the text appears normally, when I change the stage to 390x 694 with content scale enabled, the children do not change and the text size remains the same as at 720x1280.
how to know the font-size difference if the stage changes?
or how to make children responsive adjust the size of the stage?
Previously I used scaleX and scaleY to set the font size but it only worked in one frame.

Actionscript: Multiline text fields and bottom padding

I am creating a text field using
multiline = isMultiLine;
wordWrap = true;
I am setting the height of the text field using
_tf.height = _tf.height * _tf.numLines;
Because if I do not not the additional lines do not appear and you have to click on them the drag to see them.
The above code works, however the padding at the bottom of the _tf.height gets larger as the number of lines increase. So I think this is because my input height is getting multiplied by the number of lines.
However, I am using the minimum _tf.height value, if I reduce it then all the lines overlap.
I also tried manually setting the _tf.height later on to remove the extra padding, however this resulted in the same overlap issue.
How can I remove the bottom padding?
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/TextField.html#autoSize
If autoSize is set to TextFieldAutoSize.LEFT ... If wordWrap is also set to true, only the bottom of the text field is resized and the right side remains fixed.
TextFiel.wordWrap = true;
TextField.autoSize = TextFieldAutoSize.LEFT;
This will work better than mulitplying by number of lines
tf.width = DESIRED_WIDTH;
tf.multiline = isMultiLine;
tf.wordWrap = true;
tf.text = yourText;
tf.height = tf.textHeight + DESIRED_PADDING;
textHeight will tell you the actual height used by the glyphs contained within. Usually you want at least a few pixels of padding or it could clip you just a bit.

How to create smoothly animated text field with custom antialias?

I'm trying to create a TextField which will look smooth enough when animated. The only option, when I can get smoothly-animated textfield, is setting antiAliasType property to AntiAliasType.NORMAL; But I'd like to play with text antialiasing to make letters a little bit thicker. So I change the antiAliasType to AntiAliasType.ADVANCED.
After that the textfield looks jumpy when animated (it looks like glyphs are being snapped to pixel-grid). Change of property gridFitType of the textField doesn't make any sense.
Has anyone achieved smooth text-field animation when antiAliasType is switched to AntiAliasType.ADVANCED? (The jumpy text occurs when switching to TLF text fields either)
Here is my short code:
var p:TextField = new TextField();
var font:Font = new Font1XXX();
// font is embedded int the library and exported as Font1XXX class
var tfor:TextFormat = new TextFormat();
tfor.font = font.fontName;
tfor.size = 15;
tfor.color = 0xFFFFFF;
p.defaultTextFormat = tfor;
p.autoSize = TextFieldAutoSize.LEFT;
p.antiAliasType = AntiAliasType.ADVANCED;
p.gridFitType = GridFitType.NONE;
// change to GridFitType.NONE does not make any sense;
p.selectable = false;
p.embedFonts = true;
p.text = "HELLO WORLD";
addChild(p);
If you don't need selectable text, draw the textfield into a bitmap and add the bitmap to the stage instead of the textfield.
Every text in flash is snapped to whole pixels, resulting in any animation in regards to them will feel "jagged".
Workarounds are to convert any "static text" into a shape and place inside movieclip or to "copy" the text into a bitmap and animate that bitmap. Depending on what type of animation you want to do, different solutions are required for best visual experience.
For instance, scrolling text, it would probably be best to cache it as bitmap and then move it along the axis.
Scaling up/down a text (if it is scaled a lot). This sucks, I haven't found a good way to resolve this without visual artifacts. Best solution is usually to copy the text into another clip, hide the text and then animate quickly or try to cover it with other effects so focus is taken away from the text itself.

flash as3 draw string with fixed width

I wanna draw a string to a sprite with a fixed with, and be able to determine the height that the string took up. To give a you a more insight, I'll be creating a simple bubble messaging interface. So I just wanna know how to draw a string to a sprite (or any object you find more suitable), be able to control the width, and get the final height.
Thanks
Update: As a matter of fact, I don't necessarily need to be drawing the string. I just need to create that interface.
Update 2: I tried creating a textfield dynamically, but the problem is that I have no idea how to determine the height of it!
Consider this sample :
var sp:Sprite = new Sprite();
var tf:TextField= new TextField();
sp.addChild(tf);
tf.multiline = true;
tf.text = "line 1 \n line 2 \n line 3";
trace(tf.textWidth);
trace(tf.textHeight);
You should be able to use the height & width of the textbox, to re size the outer sprite.

Autosize text to fit the width of a button

I've had a good look around but couldn't find anything that directly solves my problem. There are several posts which are along the right lines, but I can't get my head around it in order to fix the issue.
I need to automatically resize the text so that it doesn't exceed the width of the button. The maximum length for my sized text appears to be 13 characters before it will become too large.
--
The program starts by dynamically creating several buttons, filling them with the title and description as set in an XML document.
To add the text I have this code:
var tform:TextFormat = new TextFormat();
tform.size = 20;
tform.font = "Arial";
tform.align = TextFormatAlign.CENTER;
tform.color = 0xFFFFFF;
tform.bold = true;
var tfield:TextField = new TextField();
tfield.text = texttitle;
tfield.width = button.width;
tfield.x = 0;
tfield.y = 30;
tfield.setTextFormat(tform);
addChild(tfield);
This positions the text directly in the centre of the button 30 pixels from the top. The problem I therefore face is how to modify the font size in order to keep the text within the box.
--
Now, because I set the width to the the size of the panel, I can't see any obvious way of looping through to set the font size according to the maximum allowed width.
I trust this all makes sense, and I welcome any modifications to make the code more efficient as I'm relatively new to AS3 and Flash and therefore am still on the learning curve.
Regards,
Jon.
I've ran across this problem and solved it by looping over the text setting until it fits
something along the lines of this
while (tf.textwidth > button.width){
myTextFormat.size = myTextFormat.size - 1;
tf.setTextFormat(myTextFormat);
tf.autoSize = "left";
}