Ardor3D Wiki
UI Text in Ardor3D
Ardor3D's UI system provides several interesting features designed to give you options when displaying and entering text. First, UI widgets that display text such as UILabel, UIButton, UITextField and so forth, can declare the family of font, color, size and other style attributes they would like to use. These elements can be declared in two ways: through inheritable component properties or through markup on the text itself.
Font Styles
UIComponents have a map field called fontStyles that is used by Ardor3D's text system to describe the desired font to use for text. The map's key-value pairs are passed along to any children it might have (as in the case of a UIContainer) but can be overridden by the child component. The keys are String based and can come from the static members of the [i]com.ardor3d.extension.ui.text.StyleConstants[/i] class. The type of class to use for the key-value's value depends on the key as follows:
- KEY_BOLD: Boolean
- KEY_ITALICS: Boolean
- KEY_FAMILY: String
- KEY_SIZE: Integer
- KEY_COLOR: ReadOnlyColorRGBA
Adding an entry to a component's fontStyles map tells it (and its children) to use that value across all the characters in any text it may have (unless that is overridden by markup, see below.) It is possible to add custom key types to this list, particularly if the key is used in conjunction with a custom FontProvider.
Styled Text and Markup
Individual UI components can also use different font styles, sizes and so forth within the same string of text. This is done via adding markup to the text itself and then marking the UILabel, UITextField, etc. as being “styled text” ([i]setStyledText[/i]). Ardor3D's default markup handler understands a few simple markup commands. You may also write your own markup handler and inject it to be used instead.
The default markup uses start and end tags to specify StyleSpans. Each StyleSpan is similar to an entry in the font styles map as described above, with key and value, but also includes a start index and length describing the portion of the text that the style should be applied to.
Ardor3D's default markup is handled by com.ardor3d.extension.ui.text.parser.ForumLikeMarkupParser and includes:
- [b]bold[/b]
- [i]italic[/i]
- [size=10]size[/size]
- [f=Arial]font family[/f]
- [c=#FF0000]color[/c]
- The hex value may be 1, 2, 3, 4, 6, or 8 characters long (not including #) and maps to:
- 1: 8bit greyscale with 100% alpha [c=#F]
- 2: 8bit greyscale with 8bit alpha [c=#FF]
- 3: 8bit red, green and blue with 100% alpha [c=#FFF]
- 4: 8bit red, green, blue and alpha [c=#FFFF]
- 6: 16bit red, green and blue with 100% alpha [c=#FFFFFF]
- 8: 16bit red, green, blue and alpha [c=#FFFFFFFF]