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
Developer
Posts: 86
Registered: ‎09-01-2009
My Device: Not Specified
Accepted Solution

Font width

Hi,

 How can i get the font width in paint method.

 

Developer
Posts: 207
Registered: ‎07-31-2009
My Device: Not Specified

Re: Font width

Could u be more specific on ur query.. Off the top of my head take a look at the getClippingRect() method..
------------------------------------------------------------------------------------------------------------------------
Click the Kudos! badge on left side of the message , if you are happy with a solution given by a user. When you do so, you are saying thanks to its author.
Mark a reply as a solution , if you think your question has been answered.
To mark a message as a solution: Click "Accept as solution" icon on the reply.
Developer
Posts: 17,012
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Font width

getAdvance(String text) in the font class.
----------------------------------------------------------
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
Highlighted
Developer
Posts: 326
Registered: ‎02-02-2009
My Device: Not Specified

Re: Font width

protected void paint(Graphics graphics){
 Font _fnt = graphics.getFont();
 int _width = _fnt.getAdvance("text");
}

Developer
Posts: 86
Registered: ‎09-01-2009
My Device: Not Specified

Re: Font width

I want to know about prasent font width and font family of my component say LabelField. How can i do this task?

 

 

 

 

Thanks,

Regards,

Jitendra.Balla

 

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

Re: Font width

use getFont to retrieve the font object, it offers methods to get all related attributes. most fonts are not fixed width and glyphs have different widths. if you want to know the maximum width of a certain string you can ask for the advance of a same length string of max-width symbols, like 8888.
----------------------------------------------------------
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: 10
Registered: ‎04-09-2012
My Device: 9360
My Carrier: AIRTEL

Re: Font width

but this behaves very weired and I never seem to get the right number of characters per display width..... 

it always custs me off in between .... and I don't tend to get same look and feel for all of my rows..... 

 

if I tend to use getAdvanceText("W")

and number of chracters = display width / fontWidth ;

 

and I supply drawText(0,y, displayString.substring(0,nChars)Smiley Wink this is the out I see in my rows..... ( I need elipsis at the end of the text to say there is more text...) 

 

this is first line first row.... 

this is second line second row..... 

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

Re: Font width

Welcome to the forum.

 

It is best to start your own question rather than add to a solved question.  People like me who are trying to help ,don't generally look at solved Threads because the problem has been solved!

 

In this case you are assuming that all characters take up the same width as the the "W", which is true for non proportional fonts, but is not true for proportional fonts.  And I'm guessing you are using a proportional font. 

 

For example getAdvance("iiiiiiiiii") will be a lot smaller than getAdvance("WWWWWWWWWW"), even though there are 10 characters in both stings. 

 

I hope that explains what you see.

 

Note that you can use drawText and give it a complete String, tell it how wide the area it has to draw into, and it can put the ellipsis at the end if the entire string does not fit.

 

I think it goes something like

drawText(<string>, x, y, (DrawStyle.LEFT + DrawStyle.ELLIPSIS + DrawStyle.TOP), width );