Space character in Lua

I’ve been trying to generate a text string with a variable number of spaces so that a list of high scores ends up being aligned in a two-column format.

Score Date  
100 Sept 8  
10 Sept 8  
0 Sept 8  

(spaces equal 6 - number of digits in score for consistent spacing)

When I try to insert a numer of spaces, they don’t show up and the score and date column get out of line. Lua seems not to like a series of repeating spaces.

When I try it with another spacer character, as below, it works fine.

[lua]local spacer = string.rep("-", spaces)[/lua]

I tried using %s in place of " " but the string.rep function wants a string. And I can’t seem to find an escaped space character, like /t is for tab.

Any thoughts on how to do this right? [import]uid: 1560 topic_id: 1940 reply_id: 301940[/import]

Dotnaught,

This seemed to have worked for me.
local score = { “1000”,“100”,“10”,“1” }

for i = 1, #score do

local ss = string.rep (" " , 6 - string.len( score[i]));
print (score[i] … ss … “|”);

end

[import]uid: 24 topic_id: 1940 reply_id: 5963[/import]

That works in the terminal, but on-screen seems to be different. I’ve included a screenshot below. Note that the dates do not line up. It’s a minor aesthetic issue but it is sort of perplexing. If I use a dash character they line up. It may have something to do with the function I’ve used to autowrap the list of scores.

[import]uid: 1560 topic_id: 1940 reply_id: 5973[/import]

I would break up the two columns into scoreColumn to start at x and dateColumn to start at x’ to guarantee exact location. Fonts are funky. specifically different font sizes. monospace and proportional font glyph widths can be troublesome.

Or !!! you could do the

string.rep (" ",6 -string.len(foooooo)) <
Carlos

[import]uid: 24 topic_id: 1940 reply_id: 5977[/import]

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]

Thanks for sharing!

Carlos [import]uid: 24 topic_id: 1940 reply_id: 6073[/import]