Может кто-нибудь помочь мне понять, как переработать текст?

Так что я проверил везде, и это так сложно понять. Я пишу приложение в RubyMotion с использованием RedPotion, но по большей части мне нужна помощь с 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

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

Я считаю, что это было бы хорошим началом для дальнейшей настройки.

Другие вопросы по тегам