
How would I scale text so it fits in a box? I have been trying to do this for a while, but I can't find something that's efficient and not laggy!
Here's an example of what I have so far.
Code: Select all
function love.load()
text = "Button"
end
function love.draw()
love.graphics.setColor(1,1,1)
love.graphics.print(love.timer.getFPS(),0,0) -- To check for lag
local bx = love.graphics.getWidth()/5 -- Set box width
local by = love.graphics.getHeight()/5 -- Set box height
love.graphics.rectangle("fill", love.graphics.getWidth()/2-bx/2, love.graphics.getHeight()/2-by/2, bx, by) -- Draw box centered
love.graphics.setColor(0,0,0)
love.graphics.setFont(love.graphics.newFont(math.min(bx/4,by/3))) -- Set the font to a size that works with the current text
local tw = love.graphics.getFont():getWidth(text) -- Get text width
local th = love.graphics.getFont():getHeight(text) -- Get text height
love.graphics.print(text, love.graphics.getWidth()/2-tw/2, love.graphics.getHeight()/2-th/2) -- Draw text centered
end
Everything works about this, except if I change the text to something short, the text is too small, and if I change the text to something longer, the text is too big.
How would I make this, but it works on any text?
Any help will be appreciated!

