embedFonts when applied, dynamic text doesn't appear (must need it for FlashEff2) - actionscript-3

I don't understand, what is wrong with this simple peice of code: (FlashCS4, AS3)
var tf:TextField = new TextField();
tf.defaultTextFormat = new TextFormat( "Arial Black", 16, 0, false );
tf.embedFonts = true;
tf.text = "Hello"; //set text last
addChild(tf);
It doesn't create or show anything, and if i comment this line tf.embedFonts = true; then it works fine.
My motive is to change the font realtime, whenever user select a different font, the dynamic text field should change accordingly.
I have applied TextEff2 for some animation, and it must need embedFonts set to true.
Please advice.
Thank you!

Related

Using device fonts and embedded fonts (FontAwesome) in the same TextField

I have a TextField of type "input" and I want users to type in with "Arial" and insert "FontAwesome 5" icons.
The users would be inserting icons through the software.
However to get the Icons to work I need to embed the FontAwesome 5 Icons
I do that using
[Embed(source="../assets/Font Awesome 5 Free-Solid-900.otf", fontName="Font Awesome 5 Free Solid", mimeType="application/x-font-opentype", embedAsCFF="false", unicodeRange="U+F000-F8FF")]
private var asset:Class;
then in my code i register the font and enable embedded fonts for the text field.
Font.registerFont(asset);
var tf:TextField = new TextField();
tf.embedFonts = true;
tf.type = "input";
tf.mouseEnabled = true;
tf.defaultTextFormat = new TextFormat("Arial", 30, 0x000000, false, false, false, "", "", "left", 5, 5, 0, 0);
tf.selectable = true;
tf.appendText("Hello World!");
var format:TextFormat = new TextFormat();
format.font = "Font Awesome 5 Free Solid";
format.size = 30;
format.color = 0xFF0000;
tf.appendText(String.fromCharCode(0xf598));
tf.setTextFormat(format, tf.length - 1, tf.length);
However, I set the defaultTextFormat as "Arial" since I want the users to type in regular script.
I then append some text that should be formatted as Arial and after that set the FontAwesome format and then insert a unicode character in the TextField.
However I am unable to see the text that was formatted as "Arial" I can only see the FontAwesome Icon.
Any ideas as to whats going wrong here would be appreciated.
You can use either device fonts or embedded fonts within a single TextField, not both at the same time. This is controlled by the TextField.embedFonts property.

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.

as3 change font size as stage size changes

As it reads I want to change the font size as the stage size changes.
I have private var myFormat:TextFormat = new TextFormat();
And then when the object gets made it writes the following
_buttonText.wordWrap = true;
myFormat.size = 15;
myFormat.align = TextFormatAlign.CENTER;
_buttonText.textColor = 0x222428;
myFormat.bold = 'true';
_buttonText.defaultTextFormat = myFormat;
_buttonText.text = text;
Then in my on enter frame I want to resize the text, I have tried a couple things but nothing seems to work, currently it looks like this.
myFormat.size = stage.stageWidth / 136.53;
Thanks for any help
A TextFormat object has no effect unless applied to a TextField. Besides if the font size should be linked to the stage size then a factor size of some kind should be applied as well. At the end it looks like this:
myFormat.size = 15 * fontSizeFactor;
//_buttonText.defaultTextFormat = myFormat;this is irrelevant if size should be dynamic.
//instead text should be set then TextFormat should be set again.
_buttonText.text = text;
_buttonText.setTextFormat(myFormat);//this is dynamic
now on enter frame:
myFormat.size = 15 * fontSizeFactor;
_buttonText.setTextFormat(myFormat);//apply the format again or else nothing will happen

AS3 - setTextFormat(), defaultTextFormat not working as designed?

UPDATED with simpler snippet
I am building a Flash swf to check through some XML files using AS3.
I only have access to the Flex compiler, not the actual Flash IDE or Flashdevelop, so all my xml checking code is a single AS file, compiled into an swf. In order to display information, I create and add a TextField and call appendText() (I use += in this snippet).
The snippet below seems to not set the new format on existing text. It appears to not have any effect whatsoever on the text.
I was under the impression that setTextFormat would change the existing text and that any new text added after calling setTextFormat would use the defaultTextFormat object. This does not appear to be the case in my code. Am I incorrect in my understanding?
public function AS3Tester()
{
display_txt = new TextField();
display_txt.multiline = true;
display_txt.width = 1024;
display_txt.height = 768;
var tf:TextFormat = new TextFormat();
tf.size = 12;
tf.font = "Lucida Console";
display_txt.defaultTextFormat = tf;
display_txt.text = "Text Before setTextFormat\n"; //Should use default style tf
addChild(display_txt);
var tf2:TextFormat = new TextFormat();
tf2.size = 18;
tf2.color = 0xFF0000;
display_txt.setTextFormat(tf2);
display_txt.text += "Text after setTextFormat\n"; //previous line should use tf2, this new line should use default tf
}

AS3 TextField Fonts

I have problems setting a font in AS3. I have tried several different things using ressources from forums and questions but I can't get it to work.
Here's the code I use:
private function addContentToMovieClips(Text:String, MC:MovieClip):void
{
var myFont = new Trebuchet();
var tFormat:TextFormat = new TextFormat();
tFormat.font = myFont.fontName;
tFormat.color = 0x000000;
trace(tFormat.font);
var tf:TextField = new TextField();
tf.defaultTextFormat = tFormat;
tf.embedFonts = true;
//tf.antiAliasType = AntiAliasType.ADVANCED;
tf.text = Text;
MC.addChild(tf);
tf.width = 300;
}
In the library, I have a Font named "font2" with AS linkage "Trebuchet". I get no compiler errors and text seems to be created on the screen but nothing is displayed.
The following line is for debugging:
trace(tFormat.font);
And returns "Trebuchet MS" as expected.
I'd be very grateful if you can help me understand why this does not work!
Cheers,
Patrick
Edit: when removing
tf.embedFonts = true;
the text is displayed with the correct font. Not sure why but this does the trick for now.
You should have pasted the code where you embed the font. But since I've had the same problem, I guess you missed the embedAsCFF directive:
[Embed(source="../someFont.ttf",
fontName = "myFont",
mimeType = "application/x-font",
fontWeight="normal",
fontStyle="normal",
unicodeRange="englishRange",
advancedAntiAliasing="true",
embedAsCFF="false")]