簡體   English   中英

有人可以幫助我了解如何回收文本嗎?

[英]Can someone help me understand how to recycle text?

所以我到處都檢查過了,很難弄清楚這一點。 我正在使用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

我不確定我是否正確理解了這個問題,但這是我的建議:

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

我相信,這將是進一步調整的良好開端。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM