Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Regular Contributor
Posts: 65
Registered: ‎05-15-2011
My Device: 9700
My Carrier: Att
Accepted Solution

Using Custom Font

Hi,

 

I have a .ttf font file added ot my app resources.

I want to apply this font by the following way, but for some reason it isn't working.

 

 

try {

if (FontManager.getInstance().load(new FileInputStream(0, "textFont.ttf"), "textFont", FontManager.APPLICATION_FONT) == FontManager.SUCCESS) {

try {

int theSize = 12;


FontFamily family = FontFamily.forName("textFont");

         Font font = family.getFont(Font.PLAIN, theSize, Ui.UNITS_pt);

          theField.setFont(font);              

       } catch (Exception e) {

       Dialog.alert(e.getMessage.toString());      

       }

}

} catch (Exception ex) {

Dialog.alert(ex.getMessage.toString());

}    

 

 

Thanks!

Developer
Posts: 19,618
Registered: ‎07-14-2008
My Device: Not Specified

Re: Using Custom Font

It would be easier for us if you could tell us how you know it isn't working.  What are the symptoms?  When you have stepped through this code, which lines are not working correctly?

Regular Contributor
Posts: 65
Registered: ‎05-15-2011
My Device: 9700
My Carrier: Att

Re: Using Custom Font

I receive NullPointerException on first try-catch

Developer
Posts: 19,618
Registered: ‎07-14-2008
My Device: Not Specified

Re: Using Custom Font

[ Edited ]

Since we can be reasonably certain that FontManager.getInstance()won't give this, then this would suggest that the problem is:

new FileInputStream(0, "textFont.ttf")

 

Now I don't see this Constructor option in my java.io.FileInputStream documentation, I can only suspect you are using net.rim.device.api.io.FileInputStream, which if you read the comments, should not be used. 

 

If you are packaging the font in your resources, then I think you will find this KB article useful to be able to access it correctly:

 

You may be able to access this directly using the FileInputStream(<String>) constructor, but I have never done it this was, so I'm not sure.  If you do use the approach you are using, and not the KB  InputStream code, then can you please let us know if it works.  I recommend that you separate this code out from the load method call, so you can easily tell which line throws the NPE.

 

Regular Contributor
Posts: 65
Registered: ‎05-15-2011
My Device: 9700
My Carrier: Att

Re: Using Custom Font

Thanks, but if I use FileInputStream(String)

 

I receive an error with suggestion to change to the constructor FileInputStream(int, String)

Regular Contributor
Posts: 65
Registered: ‎05-15-2011
My Device: 9700
My Carrier: Att

Re: Using Custom Font

[ Edited ]

        }

InputStream stream = this.getClass().getResourceAsStream("Font.ttf");

        if (stream != null) {

            if (FontManager.getInstance().load(stream, "Font", FontManager.APPLICATION_FONT) == FontManager.SUCCESS){

                try{

                 FontFamily family = FontFamily.forName("Font");

int theSize = 12;

                    Font font = family.getFont(Font.PLAIN, theSize, Ui.UNITS_pt);

                    theField.setFont(font);

                } 

                catch (Exception e) {

                

                }

            }

        } else {

        Dialog.alert("null");

}

 

 

The InputStream is null for some reason...


Developer
Posts: 19,618
Registered: ‎07-14-2008
My Device: Not Specified

Re: Using Custom Font

You will notice that the KB sample I provided had the name preceded by a \.  I think the \ is significant.  Try it with this in place.

 

I presume that you have added the file Font.ttf as a resources of your projects

Regular Contributor
Posts: 65
Registered: ‎05-15-2011
My Device: 9700
My Carrier: Att

Re: Using Custom Font

I do added the font, by the way, maybe there is a font file size limit ?

Developer
Posts: 19,618
Registered: ‎07-14-2008
My Device: Not Specified

Re: Using Custom Font

The documentation says 60K or 90K depending on how you load it, 90K if you load it from a an InputStream from memory.  How big is your font?

Developer
Posts: 123
Registered: ‎07-08-2011
My Device: Torch 9800, Bold 9700
My Carrier: Bell, Rogers

Re: Using Custom Font

I've had no problem with this....

 

 

Font myFont;

if (FontManager.getInstance().load("custom.ttf", "custom", FontManager.APPLICATION_FONT) ==FontManager.SUCCESS)         

{            

try             {                

FontFamily typeface = FontFamily.forName("custom");

myFont = typeface.getFont(Font.ITALIC , 50);
        myfield.setFont(myFont);

           }            catch (Exception e)             {               }

}

 

 

There is a font size limit though in blackberry's.

 

You can get around this though. Create a Bitmap. Make it transparent and use drawText in the paint method for your BitmpField.

 

 

Bitmap image = new Bitmap(Bitmap.ROWWISE_16BIT_COLOR, cWidth, cHeight);

image.createAlpha(Bitmap.ALPHA_BITDEPTH_8BPP);
int[] transparentImage = new int[cWidth * cHeight];

image.setARGB(transparentImage, 0, cWidth, 0, 0, cWidth, cHeight);

 

 

 

BitmapField myBitmapFont = new BitmapField(image) {

protected void paint(Graphics g) {

g.setColor(Color.BLACK);

  g.drawText("Test",0,0);

super.paint(g);

}

}

 

myBitmapFont .setFont(myFont);

 

Now you can make your font as large as you like.

 

 

Berobo Inc.
Police Scanner Radio / Alarm Clock lets you listen to Police scanners in your area!
http://www.berobo.com
_________________________________________________
Please click the Like button to thank the user that has helped you and
please mark post as solved when you have found the solution to your problem.