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
Contributor
Posts: 37
Registered: ‎07-23-2011
My Device: 9700
My Carrier: Airtel
Accepted Solution

Custom font size


In my  Application needs to apply external font name(sathu.ttf).I have tried following code, But font size is not modified as my given size . can you suggest me if iam using right?

 

final class SathuLabelBox extends LabelField
{

	int color = Color.BLACK;   
	int fntSize = 18;
	int _width = 300;

	public SathuLabelBox(String caption,int TxtColor,int fsize,int width){
		super(caption);
		color = TxtColor;
		fntSize = fsize;
		_width = width;
		if (FontManager.getInstance().load("Sathu.ttf", "MyFont", FontManager.APPLICATION_FONT) == FontManager.SUCCESS) 
		{
			try 
			{
				FontFamily typeface = FontFamily.forName("MyFont");
				Font myFont = typeface.getFont(Font.PLAIN, fntSize);
				this.setFont(myFont); 
			}
			catch (ClassNotFoundException e) 
			{
				System.out.println(e.getMessage());
			}
		}
	}


	protected void onFocus(int direction){
		color = Color.GREEN;
		fntSize = 22;
		invalidate();
	}
	protected void onUnfocus(){
		color = Color.BLACK;
		invalidate();
	}
	public void paint(Graphics graphics) {
		graphics.setColor(color);
		super.paint(graphics);       
	}
	protected void drawFocus(Graphics graphics, boolean on)
	{
		if (on)
		{
			//Get the field size that is available from its manager.
			XYRect rect = this.getManager().getContentRect();         

			graphics.setGlobalAlpha(50);
		}
	}
	public int getPreferredWidth(){
		return _width;
	}
}

 

Developer
Posts: 16,838
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Custom font size

you load the font in the constructor.
when you change the fontsize variable later this does not have any effect.
to modify the font size you would have to call setFont with a new font in the matching size.
you could store the fontfamily in a variable and repeat the font creation code, or you could store font objects in different sizes, depending on the flexibility you need, or use derive with your font object as a basis.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Contributor
Posts: 37
Registered: ‎07-23-2011
My Device: 9700
My Carrier: Airtel

Re: Custom font size

I  load the font under the paint method but no luck. Please explain where I have to do the modification in which I posted above code .

Highlighted
Developer
Posts: 16,838
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Custom font size

replace
fntSize = 22;
with setFont(xxx)
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter