newText() alignment not working after updating text

I can’t figure out how to get my text to stay left aligned after it is updated…

Here is my code…

[blockcode]
local button_1 = display.newImageRect(“blankbutton.png”, 64, 40);
button_1:setReferencePoint(display.TopLeftReferencePoint);
button_1.buttonString = “1”
button_1.buttonValue = 1
button_1.x = 17 ; button_1.y = 182.5;

local txt = display.newText(“0”,0,0, native.systemFont, 30*2);
txt.xScale = 0.5; txt.yScale = 0.5;
txt:setReferencePoint(display.CenterLeftReferencePoint);
txt.x = 100; txt.y = 55;

function button_1:touch(e)
if(e.phase == “ended”) then
txt.text = txt.text … self.buttonString
txt:setReferencePoint(display.CenterLeftReferencePoint);
txt.x = 100;
end
end

button_1:addEventListener(“touch”, button_1);
[/blockcode]

Any help would be appreciated… [import]uid: 61463 topic_id: 10566 reply_id: 310566[/import]

After 4 hours of digging I found the solution deep within the forums…

You can simply paste this code on the first line of your main.lua file…


local OldnewText = display.newText

display.newText = function( text, xPos, yPos, font, size )
local actualText = text or “”
local actualFont = font or “Helvetica”
local actualSize = size or 16

local xScale, yScale = display.contentScaleX, display.contentScaleY
local sizeMultiply = 1

if xScale < 1.0 or yScale < 1.0 then
sizeMultiply = 2
end

local t = OldnewText( actualText, 0, 0, actualFont, actualSize * sizeMultiply )
t:setReferencePoint( display.TopLeftReferencePoint )

t.xScale, t.yScale = xScale, yScale
t.x, t.y = xPos, yPos or 0, 0

return t
end
[import]uid: 61463 topic_id: 10566 reply_id: 38467[/import]

oooops… this code only work for iPhone… not iPhone 4…

Back to the drawing board.
FRUSTRATION!! [import]uid: 61463 topic_id: 10566 reply_id: 38468[/import]

Here is the iPhone 4 code that I got working… is there a way to switch between these automatically?
local OldnewText = display.newText

display.newText = function( text, xPos, yPos, font, size )
local actualText = text or “”
local actualFont = font or “Helvetica”
local actualSize = size or 16

local xScale, yScale = display.contentScaleX*2, display.contentScaleY*2
local sizeMultiply = 1

if xScale < 1.0 or yScale < 1.0 then
sizeMultiply = 2
end

local t = OldnewText( actualText, 0, 0, actualFont, actualSize * sizeMultiply )
t:setReferencePoint( display.TopLeftReferencePoint )

t.xScale, t.yScale = xScale, yScale
t.x, t.y = xPos, yPos or 0, 0

return t
end
[import]uid: 61463 topic_id: 10566 reply_id: 38481[/import]

Does anyone even read or respond to these forum posts?

[import]uid: 61463 topic_id: 10566 reply_id: 38482[/import]

Just came here to mention I have the same issue… I want to left-align text. [import]uid: 10284 topic_id: 10566 reply_id: 41156[/import]

Any update on this issue? [import]uid: 52103 topic_id: 10566 reply_id: 43540[/import]

Hey all,

There’s no simple API to align text at this stage, if you think there is an issue (your code should work but for some reason it doesn’t) then please file a bug report, then post your case number here.

byronfillmore, since you made your posts the forum has some more people around - previously questions were often missed - however if you have an urgent problem and are not getting answers then you can in touch by, as I said above, filing a bug report. (If there is an actual bug.)

Thanks,
Peach [import]uid: 52491 topic_id: 10566 reply_id: 43619[/import]

t.x, t.y = xPos, yPos or 0, 0

is not valid code…we’ll, it’s valid but does not do what you think

you really want:

t.x, t.y = xPos or 0, yPos or 0 [import]uid: 6175 topic_id: 10566 reply_id: 44641[/import]

Try removing the scaling. Scaling is centered on the reference point which might not be what you want here.

If you simply HAVE to have the scaling, when you update the text you should first reset the reference point to display.CenterReferencePoint, reset the scaling to 1, 1, change the text, THEN set the alignment with the CenterLeftReferencePoint, new x, and new scaling. Or something like that. [import]uid: 6787 topic_id: 10566 reply_id: 45108[/import]

Yep, this works, thanks snarla!
[lua]local function updateText(obj, text, x, y)
obj:setReferencePoint(display.CenterReferencePoint)
obj.xScale = 1
obj.yScale = 1
obj.text = text
obj:setReferencePoint(display.TopLeftReferencePoint)
obj.x = x
obj.y = y
obj:scale(display.contentScaleX, display.contentScaleY)
end[/lua] [import]uid: 52103 topic_id: 10566 reply_id: 45749[/import]