- Online Community Forums » Programming Questions » Centering text.

Credits go to 23yrold3yrold for helping out!
This thread is locked; no one can reply to it. rss feed Print
Centering text.
Member #8,201
January 2007

I want to put text over a graphic, like a button or whatever, and have it centered. Right now I'm just doing it manually by adding onto the x position of the text until it looks good, which is a pain in the ass.

Member #1,134
March 2001

Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Member #8,201
January 2007

Omigod omigod omigod! Are you single?

Member #1,548
September 2001

Jonatan Hedborg
Member #4,886
July 2004

Please bookmark this:

Please. :)

(oh, and the alternative method would be to use text_length(font,"the text"), and subtract half that from the x value)

Sweden: Free from the shackles of Democracy since 2008-06-18!

Member #8,201
January 2007

Top right corner. The previous question was much more then a thank you ;) but thank you. I seen this function before but never really thought anything of it at the time because I didn't need it, then I forgot about it.


That works.. But if I move the image the text was over then the text will remain in the same spot (because I'm no longer giving it the image's current x and y position.)

Nm I think.

textout_centre_ex(buffer, font, caption.c_str(), xPos + width / 2, yPos, WHITE, -1);

Member #7,733
September 2006

If you create a button, you know the button's coordinates. When you use textout_centre_ex, simply use the button's xPos,yPos coordinates, and the centered text should correctly align no matter where the button is located on the screen.

  textout_centre_ex(buffer, font, caption.c_str(), button_xPos + width / 2, button_yPos, WHITE, -1);

Go to: