Count Automatic Line Feeds

edited July 2015 in How To...

Strings automatically make a new line when they don't have enough room. I need to be able to count those automatic lines.

Tagged:

Answers

  • You already posted this question; but it wasn't clear that you were referring to wrapping happening in a text-box: as far as the String variable you're referencing is concerned there's never going to be a line break to count.

    @_vk's answer is one option: check the length of your String and divide by the max length of a line; but I suspect this may prove inaccurate.

    I don't see anything in Processing to do this; so that means looking for a pure Java solution; but I don't know enough about how Processing draws text-boxes to suggest a definitive solution. There are definitely ways to do this though...

  • _vk_vk
    edited July 2015 Answer ✓

    My suggestion isn't dividing the string length by box length because that would be inaccurate. The text box will consider each word length to fit it in the box. So this two strings, for instance, with almost same size would split differently because a big word can't be split resulting in empty spaces in first line:

    word word word word word word word word

    word word woooooooooooooord word word


    word word word word

    word word word word


    word word

    woooooooooooooord

    word word

    This is why the suggestion i made in another post adds the length of each word to a line, test if it will fit, and split the line or not...

    That code can be easily simplified and made into a simple function returning number of lines. Also it takes in count the size of the font, which will be crucial for the result.

    Another thought is to look at the source and look for text() with 5 parameters to see how this is implemented...

    edit: and the "bigger than line check" is for a

    woooooooooooooooooooooooooooooooooooooooord

    that would need to be split without having or adding spaces...

  • _vk_vk
    edited July 2015

    That code was made to keep the words in array lists for further use, doing the split by hand. In your case, perhaps you can just count the lines : )

  • Sorry @_vk: I only scanned your answer and made an assumption about how it worked. I shouldn't post answers in such a hurry :\">

  • _vk_vk
    edited July 2015

    : ) No problem at all :) I just made this comment so the OP could see that it was a different approach. Your answers are always welcome!

Sign In or Register to comment.