Can I check if I'm using an actual font when I create TextFormat/TextField ? If I specify a not embedded font or use any random string, no text is displayed and I don't know why.
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
public class SimpleSprite extends Sprite
{
//[Embed(source="C:/Windows/Fonts/Arial.ttf",fontName="default_font",mimeType="application/x-font",fontWeight="normal",fontStyle="normal",advancedAntiAliasing="true",embedAsCFF="false")]
//private static var defaultFont: String;
public function SimpleSprite()
{
var t: TextField = new TextField;
t.autoSize = TextFieldAutoSize.LEFT;
t.defaultTextFormat = new TextFormat("default_font", 16, 0xff0000);
t.embedFonts = true;
t.text = "hello world";
addChild(t);
}
}
It doesn't display any text when the embed lines are missing.
Important: My package that creates TextFields does not embed anything and I wish to keep it that way. The embedding must be done by the programmer who uses the package. I want to check if the font is embedded and throw an error if not.
You can use Font.enumerateFonts which will return an array of available embedded fonts. You could use that to create a function like the following:
private function hasEmbeddedFont(fontName:String):Boolean
{
var fonts:Array = Font.enumerateFonts();
for each(var font:Font in fonts)
{
if (font.fontName == fontName)
{
return true;
}
}
return false;
}
And then use it something like this:
t.autoSize = TextFieldAutoSize.LEFT;
t.defaultTextFormat = new TextFormat("default_font", 16, 0xff0000);
t.embedFonts = hasEmbeddedFont("default_font");
t.text = "hello world";
If you're building a library for others to use, you might consider abstracting it into your own custom subclass of TextField so it's all handled automatically.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With