Print Text in Columns

OK I give up! I’m trying to add high score functionality to the MineSweeper tutorial and I can’t get my columns to line up. Saving and restoring the scores is fine I just cant make it look pretty. I know I could just use a separate text() function for each column but I think it would be more elegant to use the string.format formatting abilities and it is killing me not being able to fix this.

According to what I have read %w.ts should format a fixed column of width w, truncated at t characters for string s. This will be right justified. If you use %-w.ts it should be left justified. So in the code below %-30.30s should print “Easy” left justified in a column 30 characters wide and it shouldn’t be truncated. It almost works but the “Medium” line doesn’t format like I would expect it to. The size of the string shouldn’t matter but it does.

I’m not sure if this is a peculiarity of the text() function, string.format or I am just misunderstanding how this works. I have tried different combinations of textAlign and textMode but can’t get the right effect.

A test stub is shown below.

I’m sure someone must have already solved this problem so any nudges in the right direction would be appreciated.

--# Main

-- Use this function to perform your initial setup
function setup()
    print("Hello World!")
end

function draw()
    
    background(0)
    
    font("Arial-BoldMT")
    fill(0,0,255)
    fontSize(72)
    textAlign(CENTER)
    
    text("High Scores", WIDTH/2, HEIGHT/2 + 220)
    
    fill(255)
    fontSize(24)
    
    local str
    
    str = string.format("%-30.30s\\t%-20.20s\\t%10d", "Easy", "Player 1", 1000)
    text(str, WIDTH/2, HEIGHT/2 + 40)
    
    str = string.format("%-30.30s\\t%-20.20s\\t%10d", "Medium", "Player 2", 2000)
    text(str, WIDTH/2, HEIGHT/2)
    
    str = string.format("%-30.30s\\t%-20.20s\\t%10d", "Hard", "Player 3", 3000)
    text(str, WIDTH/2, HEIGHT/2 - 40)
    
end

@Reefwing

You need to add textMode(CORNER), the default is center. Then you need to use a font where all of the characters are the same width, Courier.

@dave1707 - I love you man! Thank goodness, this was driving me bonkers.

You don’t have to change the font size or textMode, CENTER still works for me but I changed:

  • font to “Courier-Bold”
  • spacing and truncation to %-10.10s, this is probably a bit tight but I will play around with what is optimal keeping in mind the smaller width in portrait.

A quick google suggests that Courier is the only fixed width font on iOS. Is that right?

I don’t know if Courier is the only fixed width font, but it’s the one I always use when I want things to line up in columns. You’re correct about textMode. I tried that before I tried Courier, and then I didn’t try removing it when Courier worked. Glad things worked out.

I had a quick run through the fonts in the font selector and Inconsolata also works, but that appears to be it.

I whipped up a quick tutorial on this over at http://codeatuts.blogspot.com.au/