How to resize text to fit within a certain region.

Can I have text defined in a particular region, and as the length of the text gets bigger the fontSize reflects this length change and fits the text. So, the fontSize may change depending on the length of the text. Thanks.

Use textSize(str) after setting your properties for fontSize and font etc, it will return a vec2 so you can just put a conditional in to check if this size is bigger than your box and then set the variable for fontSize to one less.

@Luatee Actually it returns two variables, not a vec2. You need to use it like this:

local textWidth, textHeight = textSize("example")

@SkyTheCoder you’re right my mistake. That makes me wonder why

x,y = vec2(1,2) print(x,y) doesn’t work as it should (from my experience x should equal 1 and y should equal 2, instead x = vec2(1,2) and y = nil

@Luatee vec2 returns a vec2 number, not 2 seperate numbers. So when you say x,y=vec2(1,2), x contains a vec2 number and y contains nothing. So in this case, since x is a vec2, then when you do a print(x.x,x.y), you print the correct values.

@dave1707 I know that, but from my experience in different lua IDEs you can use vec2 to return two seperate values like that. Maybe it’s not standard lua maybe it’s a custom function…

But wouldn’t that defeat the purpose of a vec2 if you have two separate numbers?

@Coder, well maybe you store the information as a vec2 and then you want to read it into two variables for a specific thing like:
local x, y = pos rect(x, y, 100, 100) as opposed to rect(pos.x, pos.y, 100, 100)

@JakAttak that is how it’s used… @Coder it’s just another one of those things that helps speed up coding I guess that why it was put in. Another example would be that vec2vec2 does not exist in Codea but does in other IDEs, instead its vec2(xx1,y*y1)
Edit: it appears asterix * are for text formatting…

Ok i see now, you could make a of vec2():unpack() function that would return x and y using metatables.

@Batman Here’s an example that might help.


supportedOrientations(LANDSCAPE_ANY)

function setup()
    rectMode(CORNER)
    textMode(CORNER)    
    parameter.integer("words",6,1152)
    str="qwerty ert yvyv ybyby tvtv tvtbun ununbt ceeccr tvvtby ybybtcr rccrcr rccrcrvr"
    -- create a longer string of words
    str=str..str str=str..str str=str..str str=str..str
end

function draw()
    background(40,40,50)
    fill(255)
    
    pushStyle()
    noFill()
    stroke(255)
    strokeWidth(2)
    rect(200,350,200,50)
    
    textWrapWidth(198)
    str1=string.sub(str,1,words)
    
    size=60
    fontSize(size)
    w,h=textSize(str1)
    while h>50 do
        size=size-1
        fontSize(size)
        w,h=textSize(str1)
    end
    offset=(50-h)/2
    fill(255)
    text(str1,202,350+offset)
    popStyle()
    
    fontSize(25)
    text("How to resize text to fit within a certain region.",100,HEIGHT-100)    
    text('Move the "words" slider to increase the number of words',50,HEIGHT-200)
    text("Font size  "..size,230,HEIGHT-300)
end