Actionscript: How do I rotate a text field?

2020-06-18 03:11发布

问题:

How do you rotate a text field in actionscript 3.0? As soon as I change the rotation property of the text field, it does not display.

for example:

var txtFld:TextField = new TextField();
txtFld.x = 100;
txtFld.y = 100;
txtFld.width = 300;
txtFld.height = 300;
txtFld.text = "Test String";
txtFld.rotation = 90;
addChild(txtFld);

回答1:

Some more info supporting Christophe Herreman : ActionScript - Rotating Text



回答2:

In order to see rotated text, you'll have to embed the font.



回答3:

an alternative is, to copy the textfield into a BitmapData using BitmapData::draw and then creating a Bitmap containing the result, and adding that one to the display list, instead of the original TextField ...

this has the great advantage, that you don't need to embed the font, which reduces swf filesize ... OTOH, you will lose all of the TextField`'s interactivity, and the swf will need more RAM when playing, but the latter is not too significant ...

for the text to look smooth, set Bitmap::smoothing to true ... also, it helps, if you render your image at a higher resolution ... pseudo-anti-aliasing, so to speak ... when drawing the text, pass a Matrix scaled up by factor 2 and scale down the Bitmap by factor 2 ... that way it'll look better ...

greetz

back2dos



回答4:

I just wanted to add my experience to this question. I too wanted to rotate text.

At first, I embedded the font using only ActionScript.

Embed(source="C:\\WINDOWS\\Fonts\\CALIBRI.TTF", fontFamily="Calibri")]
public static const FONT_CALIBRI:Class;
...
var font:Font = new Global.FONT_CALIBRI as Font;
//Font.registerFont(Global.FONT_CALIBRI); //I tried various other things...

But every time I set embedFonts = true, the text would disappear. Finally I gave in and embedded the font using Flash.

var font:Font = new FontClass as Font; //FontClass was exported from Flash IDE

It finally worked.

var textFormat:TextFormat = new TextFormat(font.fontName);

textField = new TextField();
textField.defaultTextFormat = textFormat; //must be before setting the text
textField.embedFonts = true; //needed to rotate fonts
textField.autoSize = TextFieldAutoSize.CENTER;
textField.antiAliasType = flash.text.AntiAliasType.ADVANCED;
textField.text = ("TESTING")
this.addChild(textField);

Oh how I loathe using the Flash IDE for anything. If anyone was able to do this without using Flash, please do share!



回答5:

This is what worked for me.

In CS5, I needed to change a setting in the Font Embedding dialog box for it to work.

To show the Font Embedding dialog, either click the Embed button in the Character panel, or double-click a Font symbol in the Library.

Then, select the font you want to be able to rotate and click the Actionscript tab.

Finally, check the Export for Actionscript checkbox. Leave the defaults and click OK.

Below is the code that I used:

textField = new TextField();
textField.autoSize = TextFieldAutoSize.LEFT;
textField.embedFonts = true;

format.font = "Arial"; // Or whatever the name of your font is in the embed dialog
format.size = 24;
textField.defaultTextFormat = format;

addChild(textField);

If then then apply rotation to that field via AS, I still see the font.



回答6:

var txtFld:TextField = new TextField();
txtFld.x = 100;
txtFld.y = 100;
txtFld.width = 300;
txtFld.height = 300;
txtFld.text = "Test String";

txtFld.embedFonts = true; // to embed the font ... now roation works

txtFld.rotation = 90;
addChild(txtFld);