Using Custom Fonts

The customization tag for customizing fonts is BLACKBERRY_CUSTOMIZATION_POINT_FONTS.

Use custom .ttf font files, which have a maximum size of 60KB, to install and use a custom font. You can set the default font for the Hybrid Web Container (described in step 1), or change the fonts for individual labels (described in step 2).  Fonts for the list of Hybrid Apps are a special case (described in step 3).

  1. Set the default font for the Hybrid Web Container:
    1. Add the .ttf font file to the resources folder of the HybridWebContainer project.
    2. Open the HWCSettingsScreen.java file and navigate to the constructor method, and add the following code to the beginning of that method. 
      The value FELIXTI.TTF in the second line is used. This is the name of the font file, and you should replace this value with the name of the font file you added in step 1a.
      String sCustomFontName = "MyCustomFont";   
      int iFontLoadCode = FontManager.getInstance().load( "FELIXTI.TTF", sCustomFontName,
                                                          FontManager.APPLICATION_FONT);   
      if( iFontLoadCode == FontManager.SUCCESS)   
      {   
        try   
        {   
          FontFamily oFamily = FontFamily.forName( sCustomFontName );   
          Font oFont = oFamily.getFont( Font.PLAIN, 23 );   
          FontManager.getInstance().setApplicationFont( oFont );   
        }   
        catch (ClassNotFoundException e)   
        {   
          // the font was not found, so it cannot be set   
        }   
      }   
      else   
      {   
        // error loading font   
      } 
      The default font is applied to menu items, but not to the menu item that has focus. The following steps correct this.
    3. Open the AppScreen.java file and add:
      import net.rim.device.api.ui.Font; 
      import net.rim.device.api.ui.FontFamily; 
    4. Add this code to the end of the makeMenu method:
      try   
            {   
              
              FontFamily oFamily = FontFamily.forName( "MyCustomFont" );   
              Font oFont = oFamily.getFont( Font.PLAIN, 23 );   
              menu.setFont( oFont );   
            }   
            catch ( ClassNotFoundException e )   
            {   
               // problem finding the custom font   
               String errormsg = e.getMessage();   
            } 
    5. Open the LogScreen.java file and add:
      import net.rim.device.api.ui.FontFamily; 
      import net.rim.device.api.ui.component.Menu; 
    6. Add the following method to both the LogScreen class (in LogScreen.java) and to the HWCSettingsScreen class (in HWCSettingsScreen.java):
      protected void makeMenu( Menu menu, int context )   
         {   
            try   
            {   
               FontFamily oFamily = FontFamily.forName( "MyCustomFont" );   
               Font oFont = oFamily.getFont( Font.PLAIN, 23 );   
               menu.setFont( oFont );   
            }   
            catch ( ClassNotFoundException e )   
            {   
               String errormsg = e.getMessage();   
               System.out.println( errormsg );   
            }   
            super.makeMenu( menu, context );   
         } 
    7. In the HWCSettingsScreen.java file, add:
      import net.rim.device.api.ui.FontFamily;   
      import net.rim.device.api.ui.Font;   
      import net.rim.device.api.ui.component.Menu; 
  2. Set the font for an individual label:
    This example shows how to change the font for the screen title. Changing the font for any label is similar.
    1. Add the font file ( a .ttf file ) to the resources folder of the HybridWebContainer project.
    2. To the AppScreen.java file, add:
      import net.rim.device.api.ui.Font;   
      import net.rim.device.api.ui.FontFamily; 
    3. If you are going to set the font on more than one label, have a helper method. Add the following method to the AppScreen class:
      public void setCustomFont( LabelField oLabel, String sCustomFontName, int iSize )   
         {   
            try   
            {            
               FontFamily oFamily = FontFamily.forName( sCustomFontName );   
               Font oFont = oFamily.getFont( Font.PLAIN, iSize );   
               oLabel.setFont( oFont );   
            }   
            catch (ClassNotFoundException e)   
            {   
               // the font was not found, so it cannot be set   
               System.out.println( "Exception: font not found!" );   
            }   
         } 
    4. In the AppScreen constructor, replace the setTitle(…) line with the code below. 
      "SHOWG.TTF" is the name of the font file. Replace this with the name of the font file you added in step 2a.
            LabelField oTitleLabel = new LabelField( Consts.APP_TITLE, DrawStyle.ELLIPSIS );    
            FontManager.getInstance().load( "SHOWG.TTF", "CustomTitleFont", FontManager.APPLICATION_FONT);   
            setCustomFont( oTitleLabel, "CustomTitleFont", 23 );   
            this.setTitle( oTitleLabel );   
               
  3. To change the font for the names of the Hybrid Apps in the list of Hybrid Apps:
    1. Add the font file ( a .ttf file ) to the resources folder of the HybridWebContainer project.
    2. Open the AppScreen.java file for editing.
    3. Navigate to the drawListRow in ListFieldCallback and make the changes below, shown in bold.  
      "HARLOWSI.TTF" is the name of the font file. Replace this with the name of the font file you added in step 3a.
      // Draw text    
      FontManager.getInstance().load( "HARLOWSI.TTF", "CustomHybridAppFont", FontManager.APPLICATION_FONT);   
      try   
      {   
                                FontFamily oFamily = FontFamily.forName( "CustomHybridAppFont" );   
                                Font oFont = oFamily.getFont( Font.PLAIN, 23 );   
                                graphics.setFont( oFont );   
                                graphics.drawText( oApp.getDisplayName(), 2 * iMargin + oBitmap.getWidth(), y );       
                             
      }   
      catch ( ClassNotFoundException e )   
                             
      {   
      //can't load the font                          
      }