Page 1 of 2

How to center text in a box?

Posted: Sun May 22, 2011 4:46 am
by afella
Hello,

I want to create simple variation of 15 puzzle http://en.wikipedia.org/wiki/Fifteen_puzzle

Image

I do not want to use pre-renderd images for buttons.

How do I draw an square with a number inside, so that number is centered (horizontally and vertically) inside that square?

Is there a way to have a text string and figure out what is the pixel size for this string on screen? Something like

height, width = love.graphics.getTextSize("Hello, world!")

Thank you!

Re: How to center text in a box?

Posted: Sun May 22, 2011 4:59 am
by BlackBulletIV
Hello and welcome!

There is indeed a way, you'll have to use the Font class to do it. First create a new font like this:

Code: Select all

font = love.graphics.newFont(24) -- whatever size you need, I suspect 24 is good
(Make sure you do this in love.load or similar; as in, only call it once.) Then to get the width and height, use this Font:getWidth and Font:getHeight:

Code: Select all

font:getWidth("Your text here")
font:getHeight() -- height never changes, but it's a good practice to use this function in case you change the size
Hope that helps!

EDIT: Also, in future use the [ code ] (without the spaces of course) for block-style code examples.

Re: How to center text in a box?

Posted: Sun May 22, 2011 7:50 am
by Taehl
Uh, that's one way to do it. Or you could just use love.graphics.printf with the AlignMode "center".

Re: How to center text in a box?

Posted: Sun May 22, 2011 8:00 am
by Robin
Taehl wrote:Uh, that's one way to do it. Or you could just use love.graphics.printf with the AlignMode "center".
Except that doesn't align vertically, does it?

Re: How to center text in a box?

Posted: Sun May 22, 2011 8:04 am
by BlackBulletIV
Taehl wrote:Uh, that's one way to do it. Or you could just use love.graphics.printf with the AlignMode "center".
True. Then you've just got to use the height to align vertically.

Re: How to center text in a box?

Posted: Sun May 22, 2011 8:38 pm
by Taehl
I thought printf aligned it vertically? Oh well, if not, it would just be a matter of adding a fixed offset to its position.

Re: How to center text in a box?

Posted: Sun May 22, 2011 9:16 pm
by Jasoco
Does getHeight() account for the height of the letter box? Or the current characters? What I mean is "a" is smaller than "l" which would have a different box than "g" and all together you'd have "alg" which would span from the top of the l to the bottom of the g. Would getHeight() return that height even if it's just "a" or "al" or "ag"? (I'm unable to test it right now)

Re: How to center text in a box?

Posted: Sun May 22, 2011 9:37 pm
by BlackBulletIV
No getHeight() doesn't account for any characters, it's a property of the font itself, because getHeight() doesn't receive any arguments (like getWidth() does). I'm not exactly sure what the number it returns relation is with the size you specify, but I do know it's the height. For example, the default font with a size of 12 has a height of 14, 14 is 16, 32 is 42, and 72 is 84, so yeah.

Re: How to center text in a box?

Posted: Sun May 22, 2011 11:15 pm
by Lafolie
I'd have thought it was the X-Height of the typeface, which is most likely specified here as the number you give when you create the font object.

Re: How to center text in a box?

Posted: Sun May 22, 2011 11:25 pm
by BlackBulletIV
That's what I thought too, but it doesn't seem so.