简体   繁体   中英

Can someone help me understand how to recycle text?

so I've checked everywhere and it's so hard to figure this one out. I'm writing an app in RubyMotion using RedPotion but for the most part the help I need here is with Ruby. The app is pretty much a book app so I'm trying to figure out how to properly store text. Basically, I have a set amount of characters per line and only 17 lines I can use on the screen. Once that fills up, I want the rest of the text to be stored for use with the same method to appear the next set onscreen when the page flips. Then if the user swipes back, for that text to go back as well. I've tried arrays, hashes. Different methods. Been going crazy for about 3 weeks on this one problem. Anyone can help with a ruby method or tweak mine to work?

def on_load
 @texts = [
            "In the beginning of God's creation of the heavens and the           earth.\nNow the earth was
            astonishingly empty, and darkness was on the face of the deep, and the spirit of God was
            hovering over the face of the water.\nAnd God said, 'Let there be light,' and there was light.
            \nAnd God saw the light that it was good, and God separated between the light and between the darkness.
            \nAnd God called the light day, and the darkness He called night, and it was evening and it was morning,
            one day.\nAnd God said, 'Let there be an expanse in the midst of the water, and let it be a separation
            between water and water.'"
          ]


  @recycle = [ @texts[ 0 ] ]

  @page_idx = 0


  @header = append!( UIImageView, :header )
  @text_view = append!( UITextView, :text_view )
  text_view( @texts[ 0 ] )

   @text_view.on(:swipe_right) do |sender|
     recycle_text_forward( @recycle[ -1 ] )
   end

   @text_view.on(:swipe_left) do |sender|
     recycle_text_back( @recycle[ @page_idx ] )
   end
end

def text_view( text )
   page_words = text.split( ' ' )

   number_of_lines_idx = 17
   each_line_chars = 27
   chars = ""
   setter = ""
   idx = 0
   all_idx = page_words.length - 1

   @text_view.text = ""

   while number_of_lines_idx != 0
     until chars.length >= each_line_chars
     break if page_words[ idx ] == nil
     chars << page_words[ idx ] + " "
     chars << "\n" if chars.length >= each_line_chars
     idx += 1
   end

   break if page_words[ idx ] == nil

   number_of_lines_idx -= 1

   if number_of_lines_idx == 0
     @recycle << page_words[ idx..all_idx ]
     @page_idx = @recycle.length - 2
   end

   setter = chars
   chars = ""
   @text_view.text += setter
  end
end

def recycle_text_forward( text )
 text_view( text.join( ' ' ) )
end

def recycle_text_back( text )
 text_view( text )
end

I am not sure I understood the question properly, but here is what I could suggest:

input = "In the beginning of God's creation..."

_, lines = input.split(/\s+/).each_with_object(["", []]) do |word, (line, lines)|
  if line.length + word.length + 1 <= 27
    line << " " << word
  else
    lines << line.dup
    line.replace(word)
  end
end #⇒ Here we got an array of lines, now split by screen size:

lines.each_slice(17).map { |*lines| lines.join(' ').strip }
#⇒ ["In the beginning...", "and it was evening and"]

I believe, this would be a good start for further tweaking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM