Artemix
Artemix

Reputation: 8655

How to get the "style" from a classic textField when loaded by code?

I need to get the style used in a textField placed in the stage, and doing this by code.

The thing is, bold and italic are not enough in my case, since I have a style that is semi-bold.

And I can't find the way to get that style when I analyze the textField in my code.

Do you know of any way to do this?.

In other words, I need to know what is the style of the font used in Flash when I get the textFormat.

For example, if I have a font that is "Signika", I need to know that the style is Semibold. Right now, I only get "Signika". It works if I check the bold style, becuase bold is true. But thats all the information I can get from it.

Thanks.

Upvotes: 0

Views: 671

Answers (1)

Atriace
Atriace

Reputation: 2558

Behold TextField API Reference and the getTextFormat() method. (yay!)

It returns a TextFormat object which contains a complete definition of that TextField, which can then be applied to another TextField via setTextFormat().

That said, font families work a little differently in AS3 (sadly, not Adobe's fault, but rather the OS). For example, Myriad Pro has Regular, Light, Semibold, and Bold. Yet, once embedded, only two actual font names are displayed, and the only way to differentiate them is by setting the TextFormat.bold = true.

The problem with getting font families in flash.

Interestingly, omitting the embed of one of these companion fonts will not throw an error, but rather, in the case that you were to embed only Myriad Pro Regular and not Myriad Pro Bold, setting the TextFormat.bold = true will perform faux bold on regular (which will naturally be much lower quality than the actual bold font, but... it works).

Hope that clears things up.


Update

I looked into the Signika, and it apparently suffers from an acute identity crisis. To exemplify this, embed the entire font family and run the following:

var fontList:Array = Font.enumerateFonts();
for( var i:int = 0; i < fontList.length; i++ ) {
    trace("Font Name: " + fontList[i].fontName + ", " + fontList[i].fontStyle )
}

This will trace a list of each font that's present in the environment. Sadly, regardless of how you embed the font (programmatically, or via library import), the font overwrites other family entries (hypothetically because they report the same way without distinction to the environment).

// Myriad Pro Results:
Font Name: Myriad Pro, regular
Font Name: Myriad Pro, bold
Font Name: Myriad Pro Light, regular
Font Name: Myriad Pro Light, bold

Running the same query against a the Myriad Pro family actually reports four entries, without overlap, whereas Signika will only report 2 of 4.

// Signika Results:
Font Name: Signika, regular
Font Name: Signika, bold

What I stated previously still stands: using the API methods above will result in a TextFormat object that fully describes the fonts in use. These are distinguished by both name:String, and bold:Boolean properties on the object. However, since Flash overwrites the embedded object with the same reported properties (or simply doesn't overwrite), you only have 2 of the 4 family styles available at any time: a regular version, and a bold version.

Hope that clarifies things.

Upvotes: 1

Related Questions