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

Adobe AIR Development

Reply
Developer
Posts: 166
Registered: ‎03-04-2011
My Device: Playbook
Accepted Solution

Align text vertically on a Button

I want to center vertically a text inside a button.

 

Here is my code:

 

 

var playTextFormat:TextFormat = new TextFormat();
playTextFormat.color = 0xAA0000;   
playTextFormat.size = 18;  
playTextFormat.italic = false;  
playTextFormat.align = TextFormatAlign.CENTER;

var playText:TextField = new TextField();
playText.text = "Play";
playText.setTextFormat(playTextFormat);

var playButton:Button = new Button();
playText.width = playButton.width;
playText.height = playButton.height;
playButton.addChild(playText);		
playButton.addEventListener(MouseEvent.CLICK, playWindow);
playButton.x = (vStageWidth - playButton.width)/2;
playButton.y = (vStageHeight - playButton.height)/2;

 I have centered horizontally, but not vertically.

 

How can I do that?

 

 

Highlighted
Developer
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Align text vertically on a Button

hey vans,

 

have you looked into the LabelButton yet? it does what you need to be done:

 

http://www.blackberry.com/developers/docs/airapi/1.0.0/qnx/ui/buttons/LabelButton.html

 

hope that helps. good luck!

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Developer
Posts: 166
Registered: ‎03-04-2011
My Device: Playbook

Re: Align text vertically on a Button

Has LabelButton vertical align?

 

I was looking that class, but I haven't found anything releated to vertical alignment.

Developer
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Align text vertically on a Button

the label button sets the text via the .text property and it does all the aligning for you. so simply doing this:

 

 

var myBtn:LabelButton = new LabelButton();

myBtn.text = "Sample Text";
myBtn.setSize(150,52);

addChild(myBtn);

 

Will create a button with vertically and horizontally aligned text. hope that helps. good luck!

 

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Developer
Posts: 166
Registered: ‎03-04-2011
My Device: Playbook

Re: Align text vertically on a Button

If I use a LabelButton, can I use TextFormat?

Developer
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Align text vertically on a Button

yes you can. there is a method setTextFormatForState() that can help you with that. more detail can be found here:

 

http://www.blackberry.com/developers/docs/airapi/1.0.0/qnx/ui/buttons/LabelButton.html#setTextFormat...

 

hope that helps. good luck!

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman