Wrap Text

Send Snippet To: Save this snippet to Code Collector Pro -- view all roger's snippets
language: Other
license: Other

Code for Snippet:

                
        pg.wrap_text =function(pg, str,wrap_point, xPos, yPos)
            local currentText=str.." "
            local tmpStr=""
            local skip_amt=0
            local x=1
            local total_Skip=0
 
 
 
            local total_Text_Length=string.len(currentText)
 
            if string.len(currentText)<=wrap_point then
                string.gsub( currentText, "%(", "[" )
                string.gsub( currentText, "%)", "]" )
                pg.show(pg,currentText) 
                return
            end
 
            tmpStr=string.sub(currentText,1,wrap_point)
 
            yPos=yPos+12
 
            while string.len(tmpStr)>0 do
 
 
                --find natural wrap where space located
 
                x=1
                while(x==1)
                    do  -- strip spaces from begining of line
                    if string.sub(tmpStr,1,1)==" " then 
                        tmpStr=string.sub(tmpStr,2)
                    else
                        x=2
                    end   
                end    
 
                 x=1   
               -- if (string.len(tmpStr)~=wrap_point   then
                  while(x==1)
                    do  -- locate where to wrap
                    if (string.sub(tmpStr,-1)~=" " or string.len(tmpStr)>wrap_point) then 
                        tmpStr=string.sub(tmpStr,1,string.len(tmpStr)-1)
                        if tmpStr=="" then x=2 end
                    else
                        x=2
                    end   
                  end      -- while loop to define wrap point
              --  end    
                    tmpStr=string.gsub(tmpStr , "%s$", "") -- trim space at end
 
                    if tmpStr=="" then return end  -- exit function here when done
 
                    skip_amt=(string.len(tmpStr))
                    total_Skip=(total_Skip+skip_amt)+1
                    --print(tmpStr)
 
                    yPos=yPos-12
                    pg.begin_text()
                    pg:set_font(times, 12)
                    pg.set_text_pos(pg,xPos, yPos)
                    tmpStr = string.gsub( tmpStr, "%(", "%[" )
                    tmpStr= string.gsub( tmpStr, "%)", "%]" )
                    pg.show(pg,tmpStr) 
                    pg.end_text()
 
                    tmpStr=string.sub(currentText, total_Skip)
 
                end -- while tmpStr>0
 
            end -- eof wrap_text
comments powered by Disqus

Info

Link to this snippet:


Download to Code Collector

To use the direct link to your snippet on CodeCollector.net either copy the html from the above section or drag the Download to Code Collector to where you would like to use it.

More Info:

Times Viewed: 82
Date Added: 2018-02-01 15:23:51
Last Modified: 2018-02-01 09:24:07

Web Analytics