Hello,
I'm having some problems with positioning text for buttons. Apparently, I'm not understanding anchor points correctly (at least not for text, especially the "y" component). What I tried doing in the code below is center the text on the button. What I did was:
-create a button from an image
-set the anchor point to (0.5, 0.5)
-set the position to ( conf.width * 0.5 , conf.height * 0.6667 )
-create text with the class TextWrap with a field width of the width of the button image and alignment = "center"
-set the anchor point to (0.5, 0.5)
-set the position to ( conf.width * 0.5 , conf.height * 0.6667 )
I assumed that since I gave both the image and the text an anchor point at its center and the same position, the text would be centered on the button.
Alas, as you can see in the attached screenshot, that's not what I got. Though the text appears to be centered on the button, left to right, it APPEARS that vertically, it is centered on the top edge of the image.
I added a couple of print statements to see the location of the anchor points. Those statements are below with their output below. Below them is the output produced by those print statements.
Can someone tell me why this isn't working as I expected it to?
And can someone tell me how to align the text vertically to the center of the button?
--Start Button (image)
local bitmap = Bitmap.new(Texture.new("images/rectangularButton.png", true))
local startButton = ButtonRect.new(bitmap)
startButton:setAnchorPoint(0.5, 0.5)
startButton:setPosition( conf.width * 0.5, conf.height * 0.6667 )
backgroundLayer:addChildAt(startButton,2)
--Start Button (text)
local buttonText = TextWrap.new("Go to the Game", startButton:getWidth(), "justify", 5, font.r36)
buttonText:setAnchorPoint(0.5, 0.5)
buttonText:setPosition( conf.width * 0.5, conf.height * 0.6667 )
buttonText:setTextColor(color.red)
backgroundLayer:addChild(buttonText)
--print statements for debugging
print("startButton anchor points: ", startButton:get("anchorX"), startButton:get("anchorY"))
print("buttonText anchor points: ", buttonText:get("anchorX"), buttonText:get("anchorY"))
--print statement output (note: the image file is 501x56)
startButton anchor points: 250.5 28
buttonText anchor points: 96.5 14.5
Comments
In doing some further research I found a post from November 2012, In that post (after saying, "The anchor point for textfields is at the bottom left and cannot be changed." ), John26 shared a very short and simple function:
function centreAt(sprite,x,y)
local w=sprite:getWidth()
local h=sprite:getHeight()
sprite:setPosition(math.floor(x-w/2),math.floor(y+h/2))
end
I put this function in my config.lua file. I call it with centreAt(a, b, c), where
a=the name of the textfield, b and c are the x and y where I want the center of the textfield to be. I've already tried it and it works wonderfully.
Likes: antix
Likes: antix
https://play.google.com/store/apps/developer?id=razorback456
мій блог по гідерос https://simartinfo.blogspot.com
Слава Україні!
Alternatively, new TextField layout parameters include a way to specify the reference point to be used when drawing. Use TLF_REF_TOP to instruct gideros to use text top as a reference when drawing.
Likes: SinisterSoft, antix, totebo