Michael Clewley, Editor
Version 3.7 of the Java Development Environment (JDE), for color BlackBerry handhelds, introduces many enhancements to the user interface (UI).
Among the enhancements is the ability to specify fonts for your applications. The current release of the JDE supports three types of fonts:
- Millbank
- Millbank Tall
- System
As a developer, you can programmatically specify a default font to be used by your application. To accomplish this, we use a combination of methods from the FontFamily class and the Font class, both of which are included in the net.rim.device.api.ui package.
// Get a list of the system font families
// in the simulator this will return Millbank,
// Millbank Tall, and System.
FontFamily fontFamily[] = FontFamily.getFontFamilies();
// Create a font instance using the first
// element from the font family listing.
// In this case it will be Millbank
//
// With the getFont() method the style and
// font height must alsobe specified.
Font font = fontFamily[0].getFont(FontFamily.SCALABLE_FONT,16);
// Set the default font to be used in the
// application. Now all text will be display
// using this specific font, including
// fields and menus.
Font.setDefaultFont(font);
Before the change in default font
After the change in default font
You can now create an application with a specific font type and you will not have to be concerned about the formatting of your text and fields due to user-specific font settings.
As an alternative to working with fonts, the RichTextField offers the ability to format text to a specific font. There are two constructors for RichTextField that will accept a Font parameter.
RichTextField( String text, int[] offsets,
byte[] attributes, Font[] fonts, long style)
RichTextField( String text, int[] offsets,
byte[] attributes, Font[] fonts, Object[] cookies, long style)
Using similar code from the previous example, we will create a simple rich text field and modify the text in that field to be formatted for our specific font.
//The RichTextField constructor will only accept
//an array of fonts. For this example, it will
//be kept simple and just create a font array
//with one element.
Font fonts[] = new Font[1];
//Assign the font (Millbank) to the first
//element of the font list.
fonts[0] = font;
String text = "This is a font formatting test";
//A formatted rich text field must contain a
//list of offsets which define the boundaries or
//the formatting regions. The first offset marks
//the beginning of the field's text (normally
//0), and the last offset in the list marks the
//end of the field's text.
int offset[] = new int[2];
offset[0] = 0;
offset[1] = text.length();
//An attribute list must also be passed into the
//constructor. This is related to the regions
//described in the offset list. The attribute
//list will have one less element than the
//offset list. Each element's value corresponds
//to an index in the fonts list.
byte attribute[] = new byte[1];
attribute[0] = 0;
RichTextField richTextField = new RichTextField
(text, offset, attribute, fonts, RichTextField.TEXT_ALIGN_HCENTER);
Note: It is also acceptable to pass a null value into the offset and attribute values. This will format the entire rich text field using the font in the first element of the font array.
As seen in the image above, only the font in the rich text field has been modified and the title and menu remain as the current system default font.
Please remember that these font classes are only included with the 3.7 APIs, and will cause errors when the same application is run on a BlackBerry running version 3.6 or earlier. Be sure to code around the font classes if running your application on version 3.6.