0
votes

I'm building a component and need to be able to select fonts from a font list. I have the font list showing up but I'm unsure of what the proper datatype is or how I should be setting it. I've tried String and Font and I seem to be getting an error.

private var _tfFormat:TextFormat;
_tfFormat = new TextFormat();

This will produce an 1067: Implicit coercion of type String to unrelated flash.text:Font.

private var _font:Font = null;
_tfFormat.font = font.fontName;
[Inspectable(type="Font Name", name="font", defaultValue="Arial")]
public function get font():Font 
{
    return _font;
}

public function set font(value:Font):void 
{
    _font = value;
    invalidate();
}

This gives me a 1065 Variable is not defined.

private var _font:String = "";

var __cls:Class = getDefinitionByName(font) as Class;
var __fnFont:Font   = new __cls() as Font;              
_tfFormat.font  = __fnFont.fontName;

[Inspectable(type="Font Name", name="font", defaultValue="")]
public function get font():String 
{
    return _font;
}

public function set font(value:String):void 
{
    _font = value;
    invalidate();
}

I feel I'm pretty close and it's something ridiculously easy that I'm overlooking. Any set of eyes would be appreciated. Thanks.

1
Well in the first chunk of code, you've got value in your setter function defined as a Font while it should be a String, hence the implicit coercion error. As for the second one, I could be wrong, but perhaps you should define those variables as private var rather than just var. Even if it doesn't fix the problem it's generally better to specify the scope. Also I don't know if you did it intentionally or not, but __cls and __fnFont have two underscores.puggsoy
Thanks for the reply, the formatting and scope(s) are fine. I just didn't post the whole class, double underscores are part of our coding standards to make "reading" the code quicker/easier.gin93r

1 Answers

0
votes

Ok I solved the issue.

I changed the data type of the font back to String. As it turns out, if using an embedded font, Flash adds an asterisk to the name; eg: ArialBold* But that asterisk isn't included in the linkage identifier for the font. So in my setter I removed the asterisk and then create a class from string as normal. Here's the (shortened) code.

If there's a better way, I'm still all ears. :)

private var _font:String = "";
private var _tfFormat:TextFormat;
_tfFormat = new TextFormat();
var __cls:Class = getDefinitionByName(font) as Class;
var __fnFont:Font = new __cls() as Font;
_tfFormat.font = __fnFont.fontName;

[Inspectable(type="Font Name", name="font", defaultValue="")]
public function get font():String 
{
    return _font;
}

public function set font(value:String):void 
{
    _font = value.split("*").join('');
    invalidate();
}