How do I add text to a movie?

There are two types of text in a movie: static text which is used to display strings and dynamic text which is used to create editable text fields which are used to set variables which can then be used later in the movie or submitted to a server application.

Static text fields are created using the FSTextConstructor to generate the text and font definitions:

FSDefineText2 text = texter.defineText(identifier, str, size, color);
FSDefineFont2 font = texter.defineFont();

Notice that the text is defined before the font. The FSTextConstructor keeps track of the characters displayed so when the font object is generated it only contains the glyphs that were actually used - keeping the encoded movie as small as possible.

You can generate the text definitions after the font but you must still declare which characters will be included in the font:

texter.willDisplay(str.toCharArray());

The easiest way is to define a character set for the movie you are creating which contains all the characters you might use. The FSCharacterTable class can be used to generate a complete character set for different locales:

Locale locale = new Locale("es","ES"); font.willDisplay(FSCharacterTable.characterSetForLocale(locale));

Character sets are defined for several European countries, primarily to demonstrate the concept since the list of possible Locales is rather large. However it is easy to create new sets if your Locale is not included.

The result is that the font might contain glyphs for characters that were not actually used however it simplifies the process of handling fonts.

Dynamic Text Fields

Dynamic text fields are much easier to use than static ones. Rather than deal with glyphs, the actual string displayed is set in the FSDefineTextField object. You still need to deal with FSTextConstructor class to calculate the bounding box for the field and to generate the font definition however.

texter.willDisplay(str.toCharArray());

FSBounds bounds = texter.boundsForText(str, size);
FSDefineFont2 font = texter.defineFont();

FSDefineTextField field = new FSDefineTextField(identifier, bounds);
field.setFontHeight(size);
field.setFontIdentifier(font.getIdentifier());
field.setUseFontGlyphs(true);
field.setColor(color); field.setInitialText(str);

FSDefineTextField contains a large number of attributes that control the appearance of the text field.

Attributes
wordWrap Indicates whether the text should be wrapped.
multiline Indicates whether the text field contains multiple lines.
password Indicates whether the text field will be used to display a password.
readOnly Indicates whether the text field is read only.
selectable Indicates whether the text field is selectable.
bordered Indicates whether the text field is bordered.
HTML Indicates whether the text field contains HTML.
autosize Indicates whether the text field will resize automatically to fit the text entered.
maxLength The maximum length of the text field. May be set to zero is not maximum length is defined.

If you set the following attributes:

field.setReadOnly(true);
field.setSelectable(false);
field.setBordered(false);

then the dynamic text field will be identical in appearance to a static one. Unless you need control over layout and are displaying more than one font face in a block of text then using dynamic text fields is often easier than using static ones.