0

所以我到处检查过,很难弄清楚这一点。我正在使用 RedPotion 在 RubyMotion 中编写一个应用程序,但我在这里需要的大部分帮助是使用 Ruby。该应用程序几乎是一个书籍应用程序,所以我试图弄清楚如何正确存储文本。基本上,我每行有一定数量的字符,我只能在屏幕上使用 17 行。一旦填满,我希望将其余文本存储起来,以便在翻页时使用相同的方法在屏幕上显示下一组。然后,如果用户向后滑动,该文本也会返回。我试过数组,哈希。不同的方法。在这个问题上已经疯狂了大约 3 周。任何人都可以帮助使用红宝石方法或调整我的工作吗?

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
4

1 回答 1

1

我不确定我是否正确理解了这个问题,但我可以提出以下建议:

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"]

我相信,这将是进一步调整的良好开端。

于 2017-03-13T15:16:03.367 回答