Forums Stencyl

Centering Text

2 posts

Flag Post

(I suppose I could have put this in Programming, but I’m doing this in Stencyl, so…)

I’ve created an actor that I’m using a button. I know how to draw text, but I want to center that text in the middle of the button. Obviously, this means moving the text depending on it’s length. Does anybody know what the best way to do this is? (Or just what the size of each character in Newspaper size 12 is?)

 
Flag Post

There is a block that you can use when you draw the text that will get the width of the text.

Your solution (for centering it) would be the following…

Draw Text (YourText) at X: [ (X-Center of Button) – ( (Width of text (YourText) ) / 2 ) ] Y: [ (do the same using Y and height of current font blocks to center up/down) ]

If you’re having trouble reading that in text form, look at the following image, but use the “X Center” or “Y Center” for the button instead of the “Screen Width/Height / 2” blocks.

http://community.stencyl.com/index.php?action=dlattach;topic=1994.0;attach=1747;image