## [SOLVED] Is there a way to get the string width and height?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
YounYokel
Prole
Posts: 33
Joined: Thu Oct 03, 2019 5:57 pm
Location: Kazakhstan

### [SOLVED] Is there a way to get the string width and height?

Hey is there a way I can get the string width or height instead of font width and height only?
Last edited by YounYokel on Sat Oct 19, 2019 4:57 pm, edited 1 time in total.
Let's make games with love.

Code: Select all

string.format('Made with LÖVE %s.%s (%s)', ({love.getVersion()})[1], ({love.getVersion()})[2], ({love.getVersion()})[4])

zorg
Party member
Posts: 2938
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

### Re: Is there a way to get the string width and height?

Font:getWidth gets the width of a given text string in a specific font; Font:getHeight gives you the height of one line in that specific font. Not sure what else you might mean, unless you're asking for the number of characters (or codepoints) in a string, because that doesn't really have a width nor a height, those depend on the font used.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

YounYokel
Prole
Posts: 33
Joined: Thu Oct 03, 2019 5:57 pm
Location: Kazakhstan

### Re: Is there a way to get the string width and height?

zorg wrote:
Sat Oct 19, 2019 1:02 pm
Font:getWidth gets the width of a given text string in a specific font; Font:getHeight gives you the height of one line in that specific font. Not sure what else you might mean, unless you're asking for the number of characters (or codepoints) in a string, because that doesn't really have a width nor a height, those depend on the font used.
Oh is that it? Is it important to create a new font before using this function (even if this font is default)? ~ cause normally I don't use any other fonts so I just don't change it.

Code: Select all

font = love.graphics.newFont(14)
{
love.graphics.setFont(font)
local s="Hello, World!"
local sw,sh=font:getWidth(s),font:getHeight(s)
love.graphics.print(s.."\n"..tostring(sw).."\n"..tostring(sh))
}

Can't I just do something like this? :

Code: Select all

{
local s="Hello, World!"
local sw,sh=stringGetWidth(s), stringGetHeight(s)
love.graphics.print(s.."\n"..tostring(sw).."\n"..tostring(sh))
}

It would be much easier!
Let's make games with love.

Code: Select all

string.format('Made with LÖVE %s.%s (%s)', ({love.getVersion()})[1], ({love.getVersion()})[2], ({love.getVersion()})[4])

zorg
Party member
Posts: 2938
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

### Re: Is there a way to get the string width and height?

You don't need to create a new font, you can get the default font by just calling love.graphics.getFont.

My point was that that's still a font, because strings are abstract in the sense that without a font, they don't have width or height, just count (codepoint count if we're talking utf-8 or similar).
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

YounYokel
Prole
Posts: 33
Joined: Thu Oct 03, 2019 5:57 pm
Location: Kazakhstan

### Re: Is there a way to get the string width and height?

zorg wrote:
Sat Oct 19, 2019 2:06 pm
You don't need to create a new font, you can get the default font by just calling love.graphics.getFont.

My point was that that's still a font, because strings are abstract in the sense that without a font, they don't have width or height, just count (codepoint count if we're talking utf-8 or similar).
OK. Thanks for helping me!
Let's make games with love.

Code: Select all

string.format('Made with LÖVE %s.%s (%s)', ({love.getVersion()})[1], ({love.getVersion()})[2], ({love.getVersion()})[4])

### Who is online

Users browsing this forum: No registered users and 36 guests