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

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.

Related

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.

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

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!

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")]

Why can't I display embedded fonts in AS3?

I have gone through all topics on Embedding fonts in AS3 I could find,a nd tried all solutions. I'm probably missing something obvious, but I don't fully understand what I'm doing so please guide me in the right direction. Many of the answers involve Flash Builder or another tool but I use FlashDevelop. No idea whether that matters.
I have this line in my Main.as:
[Embed(source = "assets/SKA_75_marul_CE_extended.ttf",
fontName = "SKA_75_marul_CE_extended",
fontWeight = "bold",
advancedAntiAliasing = "true",
mimeType = "application/x-font")]
public static var SKA_75_marul_CE_extended:String;
And this exists in the constructor of an extended Sprite called Pointer.as:
var format:TextFormat = new TextFormat();
format.font = "SKA_75_marul_CE_extended";
format.color = 0xFFCCCC;
format.size = 20;
var label:TextField = new TextField();
label.defaultTextFormat = format;
label.text = "test";
label.embedFonts = true;
label.antiAliasType = AntiAliasType.ADVANCED;
//label.setTextFormat(format); --> I tried this too, didn't work...
label.defaultTextFormat = format;
label.x += img.width + 50;
this.addChild(label);
The only way I've found to get it to display anything is if I turn off embedFonts. I've tried embedding C:/windows/fonts/arial.ttf without success.
It seems that embedding fonts is a dark art like no other and I must concede after 1 hour of struggling. Please send help.
UPDATE:
Here's the working code, turns out it was due to having the correct order of operations...:
[Embed(source="assets/SKA_75_marul_CE_extended.ttf",
fontName = "myFont",
mimeType = "application/x-font",
fontWeight="normal",
fontStyle="normal",
unicodeRange="U+0020-U+007E",
advancedAntiAliasing="true",
embedAsCFF="false")]
private var myEmbeddedFont:Class;
var tf:TextFormat = new TextFormat( "myFont", 20,0xffffff );
var t:TextField = new TextField;
t.embedFonts = true; // very important to set
t.defaultTextFormat = tf;
t.text = text;
t.x += img.width + 50;
t.width = 700;
this.addChild( t );
It's most DEFINITIVELY a "dark art" to get embedded fonts to work right. I would first check if "SKA_75_marul_CE_extended" is the actual name the font has in its metadata (I used Suitcase Fusion to extract the name). I've also seen TTF fonts that Flash simply refuses to embed (perhaps invalid metadata causes the embed system to fault). I would continue testing with a known working font until you find the actual problem in case it is a font file problem.
One thing I noticed is "public static var SKA_75_marul_CE_extended:String;"... shouldn't this be of type Class?
FlashDevelop font embed reference from someone who had issues:
http://www.flashdevelop.org/community/viewtopic.php?p=28301

Rendering text in AS3

I'm having a bit of confusion about how to render text in a pure AS3 project. There are classes like flash.text.StaticText but these are designer-only, you can't create them in code. I was half-expecting the Graphics class to have text-rendering options but alas, no.
Specifically I was going to add a label above each player's sprite with their name, health %, etc. So I expected to add a child text-element or draw text using Graphics in some way... it's read-only and should not support user-input, I just want to draw text on-screen.
You can use TextField class for this. Please check the reference. All fields and methods are self explanatory.
A possible example.
var myField:TextField = new TextField();
myField.text = "my text";
myField.x = 200;
myField.y = 200;
addChild(myField); // assuming you are in a container class
If TextField doesn't work, you can create text using this method:
var format:ElementFormat = new ElementFormat();
format.fontSize = 26;
format.color = 0x0000FF;
var textElement:TextElement = new TextElement('Hello World!', format);
var textBlock:TextBlock = new TextBlock();
textBlock.content = textElement;
var textLine:TextLine = textBlock.createTextLine(null, 500);
textLine.x = (stage.stageWidtht - textLine.width) / 2;
textLine.y = (stage.stageHeight - textLine.height) / 2;
addChild(textLine);
Look at:
Creating and displaying text in ActionScript 3.0 Developer’s Guide