Scrolling long file

Hi,
I need to display a long file and I didn’t find any example or tutorial. Being a beginner I need tutorial with working examples. I’ve tried some codes found googling but they don’t work.
Where can I get help?
Thanks
Nino

This should work for you

    local widget = require( "widget" )

    local scrollView = widget.newScrollView
    {
        top = 10,
        left=10,
        width = display.contentWidth-20, 
        height=display.contentHeight-20,
        scrollHeight = display.contentHeight,
        verticalScrollDisabled = false,
        horizontalScrollDisabled = true,
    }
local currentY=50

for i=1,100 do
	local yourText = display.newText( "Your Text Here "..tostring(i), display.contentCenterX,currentY,  native.systemFont, 20 )
	yourText:setFillColor(0)
    scrollView:insert(yourText)
    currentY=currentY+100
end

if the text is coming from a file:

    local allText=""
    local filePath = system.pathForFile( "file.txt", system.DocumentsDirectory )--you should have a text file with this name in this path
    local myFile = io.open( filePath, "r" )
    if myFile then
            allText=myFile:read("*a")
            io.close( myFile )
    end
    local optionsText = {
        text="",
        x = 0,
        y = 0,
        fontSize = 30,
        font = native.systemFont,
        align = "left"
    }
    local yourText = display.newText(optionsText) 
    yourText.text = allText
    yourText.anchorY=0
    yourText.anchorX=0
    yourText.x=50
    yourText.y=50
    yourText:setFillColor(0)
    scrollView:insert(yourText)

I Also attached an example a bit different from the code above after testing it on my PC … you can uncomment or comment codes that load text (from a loop or from a file)

ttt.zip (66.3 KB)

2 Likes

One thing to keep in mind with big blocks of text is that because Solar2D renders text as an image, if your text block is rendered at something greater than the maximum texture size for the device it’s being displayed on, it will not render and show up as blank. This is an issue that won’t necessarily show up in the simulator, and varies from device to device.

There’s a good article that explains this more thoroughly and explains how to successfully display large blocks of test here:

1 Like

Thank you. I read that article but it is not what I need. I need to scroll database table with two short fields.

I adapted you code to my project, but it does show anything. The background of my project is white. Is that important?

Here is your modified code:


local json = require( "json" )

local widget = require( "widget" )

local scrollView = widget.newScrollView
{
    top = 10,
    left=10,
    width = display.contentWidth-20, 
    height=display.contentHeight-20,
    scrollHeight = display.contentHeight,
    verticalScrollDisabled = false,
    horizontalScrollDisabled = true,
}

local allTable={}

local filePath = system.pathForFile( "file.txt", system.DocumentsDirectory )

local function loadScores()
  local file =io.open( filePath,"r")
  if file then
    local allTable=file:read("*a")
    io.close( file )
    allTable=json.decode(contents)
  end
  if (allTable==nil or #allTable==0) then
      allTable = { 10000, 7500, 5200, 4700, 3500, 3200, 1200, 11000, 800, 5000 }       
  end
end

local optionsText = {
  text="",
    x = 0,
    y = 0,
    width=scrollView.width-20,
    fontSize = 20,
    font = native.systemFont,
    align = "left"
}

 local tableRows = display.newText(optionsText) 
 tableRows.text = allTable
 tableRows.anchorY=0
 tableRows.anchorX=0
 tableRows.x=10
 tableRows.y=10
 tableRows:setFillColor(0,0,0,1)
 scrollView:insert(tableRows)
  • First thing the function you created (loadScores) is never called in your code … you have to call it like this:
    loadScores()

  • second thing the file named file.txt does not exist in your program … if there was a file in the same directory as main.lua then you have to remove system.DocumentsDirectory and the preceding comma

  • last thing is you don’t assign text from an array like this … arrays or tables have elements that need to be read via For Loops

if you download and unzip the project folder i sent you … it’s working perfectly … just try to run it … and modify whatever you want

Obviously, a button of the “menu.lua” of my project calls the function “loadScores” and, of course, I’ve put your “file.txt” in the same directory as “main.lua”!
I changed the array in a text variable and I removed “, system.DocumentsDirectory” but nothing happens.

Hi Saleh,

Why don’t you take things the easy way man.

Just run the code i sent you as an attachment, and make sure its running perfectly, then modify it and check

Hi,
your code is OK, but I have to use it in my project, so I have to adapt it.
Anyway I’ve found an alternative.
Thanks.