Get width of text printed using print

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
codinghands
Prole
Posts: 11
Joined: Fri May 17, 2013 7:40 am

Get width of text printed using print

Post 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
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: Get width of text printed using print

Post 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.
szensk
Party member
Posts: 155
Joined: Sat Jan 19, 2013 3:57 am

Re: Get width of text printed using print

Post 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
codinghands
Prole
Posts: 11
Joined: Fri May 17, 2013 7:40 am

Re: Get width of text printed using print

Post by codinghands »

You guys. <3. :getWidth() will do very nicely.

Thanks!
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 4 guests