Hello,
I want to create simple variation of 15 puzzle http://en.wikipedia.org/wiki/Fifteen_puzzle
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!
How to center text in a box?
- BlackBulletIV
- Inner party member
- Posts: 1261
- Joined: Wed Dec 29, 2010 8:19 pm
- Location: Queensland, Australia
- Contact:
Re: How to center text in a box?
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:
(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:
Hope that helps!
EDIT: Also, in future use the [ code ] (without the spaces of course) for block-style code examples.
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
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
EDIT: Also, in future use the [ code ] (without the spaces of course) for block-style code examples.
- Taehl
- Dreaming in associative arrays
- Posts: 1025
- Joined: Mon Jan 11, 2010 5:07 am
- Location: CA, USA
- Contact:
Re: How to center text in a box?
Uh, that's one way to do it. Or you could just use love.graphics.printf with the AlignMode "center".
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: How to center text in a box?
Except that doesn't align vertically, does it?Taehl wrote:Uh, that's one way to do it. Or you could just use love.graphics.printf with the AlignMode "center".
Help us help you: attach a .love.
- BlackBulletIV
- Inner party member
- Posts: 1261
- Joined: Wed Dec 29, 2010 8:19 pm
- Location: Queensland, Australia
- Contact:
Re: How to center text in a box?
True. Then you've just got to use the height to align vertically.Taehl wrote:Uh, that's one way to do it. Or you could just use love.graphics.printf with the AlignMode "center".
- Taehl
- Dreaming in associative arrays
- Posts: 1025
- Joined: Mon Jan 11, 2010 5:07 am
- Location: CA, USA
- Contact:
Re: How to center text in a box?
I thought printf aligned it vertically? Oh well, if not, it would just be a matter of adding a fixed offset to its position.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
- Jasoco
- Inner party member
- Posts: 3727
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: How to center text in a box?
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)
- BlackBulletIV
- Inner party member
- Posts: 1261
- Joined: Wed Dec 29, 2010 8:19 pm
- Location: Queensland, Australia
- Contact:
Re: How to center text in a box?
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?
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.
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
- BlackBulletIV
- Inner party member
- Posts: 1261
- Joined: Wed Dec 29, 2010 8:19 pm
- Location: Queensland, Australia
- Contact:
Re: How to center text in a box?
That's what I thought too, but it doesn't seem so.
Who is online
Users browsing this forum: No registered users and 5 guests