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: 62
Registered: ‎07-14-2008

Text alignment in drawText()

How can I get my text to be aligned automatically in paint() of my custom field. Here what I've tried and thought it would work, but the text alignment is still top left - it should be VCENTER LEFT. It worked that way with HALIGN_MASK in the CustomButtonField example.

 

protected void paint(Graphics graphics) { if (isFocus()) { graphics.setColor(Color.WHITE); graphics.fillArc(10, 3, _imageHeight, _imageWidth, 0, 360); graphics.setColor(Color.RED); graphics.drawText(_label, 15 + _imageWidth, 0, (int)( getStyle() & DrawStyle.VALIGN_MASK ), getWidth()); } graphics.drawBitmap(10, 3, _imageHeight, _imageWidth, _image, 0, 0); }

 

 

the field is created passing VCENTER style flag:

 

 

_imfItems[index] = new ImageButtonField(bmp, "Item 1", ImageButtonField.USE_ALL_WIDTH | ImageButtonField.VCENTER)

 

 

 

Please use plain text.
Developer
mreed
Posts: 1,041
Registered: ‎07-16-2008

Re: Text alignment in drawText()

You can use alignment bits (Graphics.HCENTER / VCENTER / TOP / LEFT / etc) when drawing [some things] with the graphics object, however they would be based on the coordinates you specify. If your field height is 10, and you send a y of 0 with VCENTER... it is not going to draw it at 5 because its the center of your field, it would draw at a negative number (say -3 if your font height is 6). Figure out the actual y position and send it to the drawText() method.... y = (int) ((height - fontHeight) * 0.50)
Please use plain text.
Developer
Posts: 62
Registered: ‎07-14-2008

Re: Text alignment in drawText()

Thanks Martin for the explanation!

 

Right, that's the way I did it before I looked at the CustomButton example. So I though if drawText accepts style bits as parameters it would also be "smart" enough to do alignment automatically...

Please use plain text.