that’s a good tip carlos, two spaces instead of one!!!
i tried single space but it just doesn’t work.
i tried string.format but it only works with fixed-width fonts, i.e. Courier and Zapfino. i do not like using either one.
i was puzzled with the ui labels since i started using Corona.
i still do not understand how the x, y and bounds params work together (along with the x and y properties that the label might be given)
i found very useful a post by OderWat (he found out that you have to reset the reference point after inserting new text) and ended up with my own label class
module(..., package.seeall)
function newLabel(params)
local labelText
local x,y,size,font,textColor,align
local referencePoint,widthSign
if ( params.x and type(params.x) == "number" ) then x=params.x else x=0 end
if ( params.y and type(params.y) == "number" ) then y=params.y else y=0 end
if ( params.size and type(params.size) == "number" ) then size=params.size else size=20 end
if ( params.font ) then font=params.font else font=native.systemFontBold end
if ( params.textColor ) then textColor=params.textColor else textColor={ 255, 255, 255, 255 } end
if ( params.align ) then align = params.align else align = "center" end
if ( params.text ) then
labelText = display.newText( params.text, 0, 0, font, size )
labelText:setTextColor( textColor[1], textColor[2], textColor[3], textColor[4] )
if ( align == "left" ) then
--will create left-aligned text starting at x
referencePoint=display.TopRightReferencePoint
widthSign=1
elseif ( align == "right" ) then
--will create right-aligned text ending at x
referencePoint=display.TopLeftReferencePoint
widthSign=-1
else
--will create center-aligned text at x
referencePoint=display.TopCenterReferencePoint
widthSign=0
end
labelText:setReferencePoint(referencePoint)
labelText.x=x+widthSign\*labelText.width
labelText.y=y
end
function labelText:setNewText(txt)
self.text=txt
self:setReferencePoint(referencePoint)
self.x=x+widthSign\*self.width
end
function labelText:getText()
return self.text
end
function labelText:setNewTextColor( color )
if ( color and type(color) == "table" ) then
self:setTextColor( color[1], color[2], color[3], color[4] )
end
end
return labelText
end
usage:
local lb=require("label")
local lbl=lb.newLabel{x=100,y=30,text="whatever",font="Helvetica",size=16,align="right",textColor={255,255,255,255}}
it works ok on the simulator and the device.
the only digit that appears to be slightly off is 1, but i can live with that, you can hardly notice the not pixel-perfect alignment.
the funny thing? to make a left aligned label you use the right reference point and to make a right aligned label the … left
[import]uid: 6459 topic_id: 1940 reply_id: 6017[/import]