How to make a Text fits some area

Hello!
I want to draw some text, to fit some area, by example, all the screen. I want this text to fit it dinamically, so when i wrote some letter, by example “H”, that letter will fit the entire screen. If i enter another letter, now they will resize to fit again. Thanks in advance!

@Andresan See the code I have in this link. If you have questions about it, just ask.

http://codea.io/talk/discussion/5349/how-to-resize-text-to-fit-within-a-certain-region-#Item_11

@Andresan Here’s a smaller version. Just change the “len” parameter to change the number of characters showing.


supportedOrientations(LANDSCAPE_ANY)

function setup()
    str="iwantonlyonelineforthis   "
    length=string.len(str)
    parameter.integer("len",1,length,length,calc)
end

function draw()
    background(40, 40, 50)
    fill(255)
    fontSize(size)
    w,h=textSize(str1)
    while w>WIDTH or h>HEIGHT do
        fontSize(size)
        w,h=textSize(str1)
        size=size-1
    end
    text(str1,WIDTH/2,HEIGHT/2)    
end

function calc()
    size=1000
    w=1000
    h=2000   
    str1=string.sub(str,1,len)
end

Thanks! I modified that to fit what i needed.

--Text Wrap to fullscreen
-- Based on dave1707's example
-- http://codea.io/talk/discussion/5349/how-to-resize-text-to-fit-within-a-certain-region-#Item_11

supportedOrientations(LANDSCAPE_ANY)

function setup()
    
    rectMode(CORNER)
    textMode(CENTER)    
    parameter.text("str","Hello World")
end

function draw()
    background(40,40,50)
    fill(255)
    HalfX=WIDTH/2
    HalfY=HEIGHT/2
    pushStyle()
    textWrapWidth(WIDTH)

    size=WIDTH
    fontSize(size)
    w,h=textSize(str)
    while h>WIDTH do
        size=size-1
        fontSize(size)
        w,h=textSize(str)
    end
    text(str,HalfX,HalfY)
    popStyle()
end

… But it bugs with some long words…

@Andresan Can you post the words that cause a problem. I tried a bunch of long words without any problems.

iwantonlyonelineforthis       

Consider some spaces at the end, just that way it troubles…

@Andresan Try the other program that I show above.

Works great :slight_smile: thanks