Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Find the width of a Textfield? — Gideros Forum

Find the width of a Textfield?

DudDud Member
edited April 2012 in General questions
Is it possible to find the width of a Textfield after it has been set with some text using a particular font?

Textfield is based on sprite - so should getWidth() work because currently it always returns 0 for me.

I'd like to be able to centre text and to do this I need to know its width.

Cheers

Comments

  • ar2rsawseenar2rsawseen Maintainer
    getWidth should work fine

    Can you post a snippet?
  • atilimatilim Maintainer
    Also there is a possibility that you're using an old version.
  • petecpetec Member
    As already said, getWidth should work. Are you using it before the text has been set as that will give zero. If you try this it should give width 0 before the text is set and width 52 after the text has been set.
    local textfield = TextField.new(nil, "")
    stage:addChild(textfield)
    print('textfield width',textfield:getWidth())
    textfield:setText("some text") 
    print('textfield width',textfield:getWidth())
  • DudDud Member
    ok thanks I'll check. I'm not actually using setText as I am initialising the Textfield in the new method.
  • petecpetec Member
    It should still work. This gives the width of the text "hello":
    local textfield = TextField.new(nil, "hello")
    stage:addChild(textfield)
    print('textfield width',textfield:getWidth())
Sign In or Register to comment.