[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
Before you make a thread asking for help, read this.
Post Reply
User avatar
YounYokel
Prole
Posts: 13
Joined: Thu Oct 03, 2019 5:57 pm
Location: Kazakhstan

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

Post by YounYokel » Sat Oct 19, 2019 10:23 am

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.

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

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

Post by zorg » 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.
Me and my stuff :3True 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.

User avatar
YounYokel
Prole
Posts: 13
Joined: Thu Oct 03, 2019 5:57 pm
Location: Kazakhstan

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

Post by YounYokel » Sat Oct 19, 2019 1:44 pm

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!

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

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

Post by zorg » 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).
Me and my stuff :3True 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.

User avatar
YounYokel
Prole
Posts: 13
Joined: Thu Oct 03, 2019 5:57 pm
Location: Kazakhstan

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

Post by YounYokel » Sat Oct 19, 2019 4:57 pm

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!

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests