Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Resize font to match a specific width — Gideros Forum

Resize font to match a specific width

totebototebo Member
edited November 2014 in General questions
Hi guys,

I have a feeling there is no good way of doing this, but I'd like to resize a textfield to match a pixel width without the text becoming jagged.

One solution I've tries was to create a text field, check the width, increment the size, create a new text field, check the size, and so on until I had the perfect size. This works, but sometimes crashes the app because of "too many files open".

It would be good if there was an option to change the point size of a font without first reloading it. Is there a way of doing this?

Niclas
My Gideros games: www.totebo.com

Comments

  • HubertRonaldHubertRonald Member
    edited November 2014
    Hi @totebo,

    Yes It's crashes because increase Lua Memory and Texture Memory.
    -----------------------------------------------------
    -- you can check the above with this piece of code
    -----------------------------------------------------
    local test
    local size = 80 -- in this case
    for i=1, 100 do
     test=TextField.new(TTFont.new("my_font.ttf", size),"Some text", true)
    end
    Well, a dirty and not computationally expensive solution (compared the above)
    ---------------------------------------------
    --  another solution
    ---------------------------------------------
    local spriteTxt = Sprite.new()
    local size = 80 -- font
    local widthWant = 120 -- or another than you prefer
     
    local Txt = TextField.new(TTFont.new("my_font.ttf", size),"Some text", true)
    local scaleTxt = widthWant/Txt:getWidth()
    spriteTxt:setScale(scaleTxt)
    spriteTxt:setPosition(posX,posY)
    The above requiere a pool class for than memory doesn't increase
    PoolText = Core.class(Sprite)
     
    function PoolTxt:init()
        self.pool = {}
    end
     
    function PoolText:createObject(w, txt, size)
        local b
        --if there is anything in pool take it
        if #self.pool > 0 then
            -- "BECAREFUL !!!"
            b = table.remove(self.pool)  -- "size is always the same because TTFont can't be modify"
            b:getChildAt(1):setText(txt)  -- one child (or text per sprite)
            local scaleTxt = w/b:getChildAt(1):getWidth()
            b:setScale(scaleTxt)
        else --create new object
            b = Sprite.new()
            local Txt=TextField.new(TTFont.new("my_font.ttf", size), txt, true)
            local scaleTxt = w/Txt:getWidth()
            b:addChild(Txt)
            b:setScale(scaleTxt)
        end
        --return object
        return b
    end
     
    --instead of destroying object, put it back to pool
    function PoolText:destroyObject(b)
        b:removeFromParent()
        table.insert(#self.pool, b)
    end
    I hope that this script can help you

    Likes: ar2rsawseen

    +1 -1 (+1 / -0 )Share on Facebook
  • Thanks HubertRonald! Much appreciated.
    My Gideros games: www.totebo.com
Sign In or Register to comment.