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...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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);

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

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

Back to the drawing board.

FRUSTRATION!!

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

Does anyone even read or respond to these forum posts?

Just came here to mention I have the same issue... I want to left-align text.

Any update on this issue?

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

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

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.

Yep, this works, thanks snarla!

1
2
3
4
5
6
7
8
9
10
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
views:1669 update:2011/9/28 9:01:40
corona forums © 2003-2011