Wrapping text into lines at word boundaries

后端 未结 2 355
傲寒
傲寒 2020-12-19 00:16

1) I want to auto wrap a text by words so that each line does not exceed 56 characters. Is there a method for doing this, or do I need to roll my own?

@comm         


        
相关标签:
2条回答
  • 2020-12-19 01:03

    I believe the function you are looking for is word_wrap. Something like this should work:

    <%= word_wrap @comment_text, :line_width => 56 %>
    

    You can combine that with gsub to get the indentation you desire:

    <%= word_wrap(@comment_text, :line_width => 52).gsub("\n", "\n    ") %>
    

    But you should probably move that into a helper method to keep your view clean.

    0 讨论(0)
  • 2020-12-19 01:09

    Perhaps word_wrap helper can help you.

    To indent the text you can replace \n (newline) with newline + 4 spaces.

    0 讨论(0)
提交回复
热议问题