Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Problems with getting text to wrap — Gideros Forum

Problems with getting text to wrap

ArtemisArtemis Member
edited April 2020 in General questions
I'm trying to accomplish what should be an incredibly simple task but obviously I'm not understanding how TextField Layouts are supposed to work. I'm trying to get text to wrap which as far as I could tell should just be a simple matter of setting the w parameter for the textfield objects layout to the desired max width.

The code looks like this:
self.Text = TextField.new(Font.getDefault(),str,{w = initwidth, letterSpacing = 5})
self.Text:setPosition(self.x,self.y)
self:addChild(self.Text)
where initwidth is a passed in number (244 in this case), str is just a lorem ipsum test string and the object gets added to the scene from main.lua.

Rather than working as I'd expect it to and wrapping the text once it exceeds the 244 pixel limit, the text just continues endlessly off screen like it didn't change a single thing. I've tried using the TextField:setLayout function after the fact instead of creating it with the layout as well but the results are the same. Am I misunderstanding how the Layouts are supposed to work here? Is text wrapping something I'll have to program in my own solution for or am I just simply doing something wrong?

Edit: As Mentioned down below, it seems the flags are initially set to no wrap causing the behaviour described above. The answer is a simple matter of changing the flags as you want or simply setting them to 0 as such:
self.Text = TextField.new(Font.getDefault(),str,{w = initwidth, letterSpacing = 5, flags = 0})
self.Text:setPosition(self.x,self.y)
self:addChild(self.Text)

Comments

  • rrraptorrrraptor Member
    edited April 2020
    The syntax is a bit different :)

    It must be:
    TextField.new(font,text,sample,layout)
    You are missing "sample" parameter.

    Here is a working version:
    local str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
     
    local tf = TextField.new(nil, str, "", {
    	w = 200, letterSpacing = 5,
    	flags = FontBase.TLF_JUSTIFIED|FontBase.TLF_REF_TOP
    })
    stage:addChild(tf)
    P.S. You dont need to use "Font.getDefault()", just pass nil.
  • hgy29hgy29 Maintainer
    Accepted Answer
    I think 'sample' is optional, but you should set flags, at least to 0, because the default value may be include the 'no wrap' flag since that's what older Gideros used to do.

    Dislikes: rrraptor

    +1 -1 (+0 / -1 )Share on Facebook
  • you can also use \n in your text string.
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • hgy29 said:

    I think 'sample' is optional, but you should set flags, at least to 0, because the default value may be include the 'no wrap' flag since that's what older Gideros used to do.

    Huh, makes sense but it just didn't even occur to me to try, thanks for the answer.
    MoKaLux said:

    you can also use \n in your text string.

    Yeah true but that's extremely limited in its use for a game, especially when generating the text on the go.
    rrraptor said:

    The syntax is a bit different :)

    It must be:

    TextField.new(font,text,sample,layout)
    You are missing "sample" parameter.

    Here is a working version:
    local str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
     
    local tf = TextField.new(nil, str, "", {
    	w = 200, letterSpacing = 5,
    	flags = FontBase.TLF_JUSTIFIED|FontBase.TLF_REF_TOP
    })
    stage:addChild(tf)
    P.S. You dont need to use "Font.getDefault()", just pass nil.
    Also thanks for the example code, wasn't exactly sure on how setting multiple flags would work but makes sense, also didn't know you could just pass nil for the font thanks for the info.

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • MoKaLuxMoKaLux Member
    edited April 2020

    Likes: SinisterSoft

    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.