Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How to make a Text fits some area

edited August 2014 in Questions Posts: 35

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!

Comments

  • dave1707dave1707 Mod
    Posts: 7,613

    @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
    
  • dave1707dave1707 Mod
    edited August 2014 Posts: 7,613

    @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
  • edited August 2014 Posts: 35

    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...

  • dave1707dave1707 Mod
    Posts: 7,613

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

  • edited August 2014 Posts: 35
    iwantonlyonelineforthis       
    

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

  • dave1707dave1707 Mod
    Posts: 7,613

    @Andresan Try the other program that I show above.

  • Posts: 35

    Works great :) thanks

Sign In or Register to comment.