Hello!
I'm using the technique used here to center a TextField:
http://giderosmobile.com/forum/discussion/381/center-textfield/p1Basically
(application:getContentWidth()/2) - (textfield:getWidth()/2) |
, but the text field (that is being updated when a value changes and re positioned) get's a little uncentered.
here is my code:
fontLarge = TTFont.new("fonts/Questrial-Regular.ttf", 80, true)
txtCalories = TextField.new(fontLarge, todayCalories .. " kcal")
addCalories(0)
txtCalories:setTextColor(0x00FF7A)
stage:addChild(txtCalories)
function addCalories (val)
todayCalories = todayCalories + val
txtCalories:setText(todayCalories .. " kcal")
txtCalories:setPosition((application:getContentWidth()/2) - (txtCalories:getWidth()/2), 390)
end |
I'm using a Google Web font, and can see that is uncentered both in the player on my phone (android) and on my pc (mac).
My guess is that it's an error in calculating the width of the TextField, like when the first number is a "1" is thinking that the 1 is bigger than it actually is :-/
Is there a better way to do this? I'm very, very new on Gideros.
Cheers
Comments
May be that the font kerning is in some way wrong?
https://deluxepixel.com
I attached the project with the font that doesn't work (Comfortaa-Regular), and another font that seems to work(Questrial-Regular) (you should have to change it in the code)
@sinistersoft the scaling didn't seem to work.
I think it's the font fault or is a bug concerning that specific font.
My code is attached.
Note that text is drawn from the baseline - so the boxes are actually UNDER the text. But the boxes show that your text is centered.
You should also make your background big enough to fill the space of devices that you plan on using. @ar2rsawseen 's book is excellent in this regard I used the default iPhone 320x 480 and there was a black left edge of probably 20 pixels.
function drawCenterLine()
center_line:clear()
center_line:setLineStyle(1, 0xffff00)
center_line:beginPath()
center_line:moveTo(application:getContentWidth()/2,0)
center_line:lineTo(application:getContentWidth()/2, application:getContentHeight())
center_line:endPath()
stage:addChild(center_line)
end
function drawAroundText(txtCalories)
shp:clear() -- Clear the canvas of any previous drawing it may have
shp:beginPath() -- Start all drawing
shp:setLineStyle(1, 0xff0000)
shp:moveTo(txtCalories:getX(),txtCalories:getY())
shp:lineTo(txtCalories:getX()+txtCalories:getWidth(), txtCalories:getY())
shp:lineTo(txtCalories:getX() + txtCalories:getWidth(),txtCalories:getY() + txtCalories:getHeight())
shp:lineTo(txtCalories:getX(),txtCalories:getY() + txtCalories:getHeight())
shp:closePath()
shp:endPath()
stage:addChild(shp)
end
txtCalories:setPosition((application:getContentWidth()/2) - (txtCalories:getWidth()/2), 390)
txtCalories:setTextColor(0x00FF7A)
stage:addChild(txtCalories)
--velobuff added these 2 methods
drawAroundText(txtCalories)
drawCenterLine()