Skip to main content
klinek3
Inspiring
August 10, 2010
Answered

detect font style in text field

  • August 10, 2010
  • 2 replies
  • 1781 views

Hello. I wonder if there is any way (in AS3) to detect font style of the text loaded into dynamic text field (via PHP).

Any ideas?

Regards

This topic has been closed for replies.
Correct answer Ned Murphy

It's not clear what you intend when you say style, but you can use the TextField's getTextFormat() method to determine different properties of the text.  If you have a TextField with an instance name "tf", then you can use the following to output the font...

trace(tf.getTextFormat(0).font);

2 replies

Inspiring
August 10, 2010

Whatever you load from server is totally agnostic of font styles, etc. What you load is a bunch of generic characters. Font manipulations can happen on client side ONLY.

Ned Murphy
Legend
August 10, 2010

It is possible to use actionscript's TextField.setTextFormat() to define different format elements for different portions of text in a textfield.  I don't know what you mean when you say font manipulations can ONLY be done client side.  I don't recall able to select a textfield in a running swf and change its font.

Inspiring
August 10, 2010

My vague understanding of the original post was that there was an assumption that whatever (text) comes from PHP has some information/metadata/mimeType about font attached. If my understanding is correct - it is a wrong assumption. In other words, data comes as generic (ASCII) characters - not formatted or styled text.

Information about fonts can be available on client side only (Flash, browser, etc.) - not on server side. ActionScript in a hosted swf is executed on the client side, including TextField.setTextFormat(). Thus, if a text that just came with a server request is assigned TextField instance text, font always defaults to, well, default one (Times New Roman on PC). Value of font for TextFormat of TextField instances that has no text is null.

Ned Murphy
Ned MurphyCorrect answer
Legend
August 10, 2010

It's not clear what you intend when you say style, but you can use the TextField's getTextFormat() method to determine different properties of the text.  If you have a TextField with an instance name "tf", then you can use the following to output the font...

trace(tf.getTextFormat(0).font);

klinek3
klinek3Author
Inspiring
August 10, 2010

Sorry - I meant Font Family

Anyway,  getTextFormat is ok, but what if the loaded text uses many different font families?   In that case with the use of getTextFormat(); I will receive "null".

Ned Murphy
Legend
August 10, 2010

Yes, a null would result if you don't specify which character you are trying to get the format information for and that format element varies in the text.  If you want to get all of the different fonts, then you will need to use a loop and go thru the entire string, one character at a time.