Page 1 of 1
Get width of text printed using print
Posted: Tue Aug 27, 2013 7:21 am
by codinghands
Is there any way in advance to get the width of text to be printed using love.graphics.print before printing it? Or even after.
Cheers,
Paul
Re: Get width of text printed using print
Posted: Tue Aug 27, 2013 7:29 am
by veethree
Yes. You can figure out the width of the font, Then multiply that by the length of the string. This works best if your font is
monospaced
To get the length of a string you can use #.
Code: Select all
string = "whatever"
string_length = #string
If you're trying to center the text or something similar, Perhaps you could look into printf.
Re: Get width of text printed using print
Posted: Tue Aug 27, 2013 7:45 am
by szensk
font:getWidth usage:
Code: Select all
local font = love.graphics.getFont()
local width =font:getWidth("Foobar and some other words too") --gets the width of the argument in pixels for this font
Re: Get width of text printed using print
Posted: Tue Aug 27, 2013 7:48 am
by codinghands
You guys. <3. :getWidth() will do very nicely.
Thanks!