Anchor not updating after changing button label

Hi guys,

I’m not 100% sure, but I think I found a bug. In my program, the label of a button is changed when the user clicks on an item. I use 

btn:setLabel()

to achieve that and it works. But the button, of which I want to change the label, has an anchorX value of 0. When I change the label, the anchor defaults back to 0.5. I tried to adjust this by doing this:

button.categoryBtn:setLabel("Corona SDK") button.categoryBtn.anchorX = 0

But that didn’t change it. Is anyone else having problems with this?

Try:   button.anchorX = 0

Woops I’m sorry Rob, the setlabel function was also supposed to look like: button.categoryBrn.anchorX = 0. I’ll edit my original post.

But it isn’t working.

Can you post your button creation code?

Sure,

so first I create the button:

 addScreen.categoryBtn = widget.newButton( { label = "Friend", labelColor = {default={129/255, 202/255, 195/255}, over={1,1,1}}, font = font.robotoM, fontSize = 15, onEvent = functions.openCategory, emboss = false, textOnly = true, x = centerCor.addMainRectX \* 1.375, y = centerCor.addMainRectY \* 1.15, } ) addScreen.categoryBtn.anchorX = 0

and after that in a function I change the label:

function functions.categoryItemEvent(event) if event.phase == "ended" then categorySelector = event.target.id addScreen.categoryBtn:setLabel(categorySelector:gsub("^%l", string.upper)) addScreen.categoryBtn.anchorX = 0 print("categorySelector = " .. categorySelector) end end

Hi @bramvbilsen,

Internally, widget buttons are display groups (made up of several elements). As such, they may not play nicely (by default) with a changed anchor point. Can you try setting the object’s “anchorChildren” property to true and see how that works for you?

https://docs.coronalabs.com/api/type/GroupObject/anchorChildren.html

Take care,

Brent

Hi Brent,

So I added the line and the button code looks like this now:

 addScreen.categoryBtn = widget.newButton( { label = "Friend", labelColor = {default={129/255, 202/255, 195/255}, over={1,1,1}}, font = font.robotoM, fontSize = 15, onEvent = functions.openCategory, emboss = false, textOnly = true, x = centerCor.addMainRectX \* 1.375, y = centerCor.addMainRectY \* 1.15, } ) addScreen.categoryBtn.anchorX = 0 addScreen.categoryBtn.anchorChildren = true

But that didn’t work :confused:

Any other suggestions?

Kind regards

Bram

Hi Bram,

Try flipping those two commands: anchor the children first, then set the anchor point. That might do the trick (I haven’t tested it in awhile but I seem to recall so).

Brent

That didn’t work either. But the anchor jumps to the correct place after pressing another button, I’ve got a confirm and cancel button. After one of those is tapped the anchor jumps to the correct place for some reason.

Perhaps it’s important to note that the button itself is in another display group. And that other display group is inserted into to the scenegroup.

Hmmm… one more little test: try setting (or rather re-setting) the button’s x and y after you do the anchor commands.

No luck either. I tried to place the code before and after I reset the anchor but no luck.

Hi Bram,

I did some testing and came up with a workaround. Most of this is because you set the button to be “text only” so, internally, it only renders the text object (sorry I didn’t notice this initially).

To make different labels align left, you can use the following workaround instead of setting the anchorX to 0. Just add half the button’s width to an x “edge” position (whatever you need). You’ll need to do this both when the button is created and when the label changes.

[lua]

addScreen.categoryBtn = widget.newButton(

    {

        label = “Friend”,

        labelColor = {default={129/255, 202/255, 195/255}, over={1,1,1}},

        font = font.robotoM,

        fontSize = 15,

        onEvent = functions.openCategory,

        emboss = false,

        textOnly = true,

        x = 0,

        y = centerCor.addMainRectY * 1.15,

    }

)

addScreen.categoryBtn.x = addScreen.categoryBtn.width/2 + centerCor.addMainRectX * 1.375

function functions.categoryItemEvent(event)

    if event.phase == “ended” then

        categorySelector = event.target.id

        addScreen.categoryBtn:setLabel(categorySelector:gsub("^%l", string.upper))

        addScreen.categoryBtn.x = addScreen.categoryBtn.width/2 + centerCor.addMainRectX * 1.375

        print("categorySelector = " … categorySelector)

    end

end

[/lua]

Hope this helps!

Brent

Hi Brent,

This did the job. Thank you for your time!

Should I send a bugreport for this or can we just leave it like this?

Kind regards

Bram

Hi Bram,

Good to hear it worked! I wouldn’t necessarily qualify this as a “bug” since, if you use non-text-only buttons and the anchor children approach, buttons can be positioned with anchors. But, if you want to file a report anyway to get it on the radar, feel free.

Brent

Try:   button.anchorX = 0

Woops I’m sorry Rob, the setlabel function was also supposed to look like: button.categoryBrn.anchorX = 0. I’ll edit my original post.

But it isn’t working.

Can you post your button creation code?

Sure,

so first I create the button:

 addScreen.categoryBtn = widget.newButton( { label = "Friend", labelColor = {default={129/255, 202/255, 195/255}, over={1,1,1}}, font = font.robotoM, fontSize = 15, onEvent = functions.openCategory, emboss = false, textOnly = true, x = centerCor.addMainRectX \* 1.375, y = centerCor.addMainRectY \* 1.15, } ) addScreen.categoryBtn.anchorX = 0

and after that in a function I change the label:

function functions.categoryItemEvent(event) if event.phase == "ended" then categorySelector = event.target.id addScreen.categoryBtn:setLabel(categorySelector:gsub("^%l", string.upper)) addScreen.categoryBtn.anchorX = 0 print("categorySelector = " .. categorySelector) end end

Hi @bramvbilsen,

Internally, widget buttons are display groups (made up of several elements). As such, they may not play nicely (by default) with a changed anchor point. Can you try setting the object’s “anchorChildren” property to true and see how that works for you?

https://docs.coronalabs.com/api/type/GroupObject/anchorChildren.html

Take care,

Brent