- Home /
How would I calculate the number of lines a string will be
Hello, everyone. I was wondering if there was a way to calculate the number of lines a string will be within a given width. The exact situation is I want the user to be able to type multiple lines into a text area and for the scroll view to adjust accordingly in length.
Answer by Uzquiano · May 03, 2011 at 09:39 PM
Hi,
Well, if you use String.Length , you can get the number of characters the string has. So, if the width of the text field is given, so just divide :)
Hope this can help you
That will only give you a rough estimate since characters are not equally spaced on a line. For example "l" takes up less space than "w"
Well... $$anonymous$$aww...
I think all chars are the same, take a look to my previous example, the three points are just under the others...
Sorry, but the comments do keep the EOL... but you could try
If you use a monospace font, the chars all have the same width. No one uses a monospace font (because the chars all have the same width, which looks bad.)
Answer by Molix · May 04, 2011 at 02:08 AM
GUIStyle.CalcHeight will give you the pixel height of the content (i.e. the string) if drawn at a certain width with the style.
Answer by Berenger · Mar 29, 2012 at 11:42 PM
That's a pretty old question but I was browsing the scrollview tag. As it happen, I made a script for a chat. It splits strings instead of wrapping. That way, if you know the size of the SV and the height of a line, you only display those visible and can have 100k+ lines.
Anyway, the code : http://pastebin.com/ZFDd8s6U
Maybe it can help someone.
Your answer
Follow this Question
Related Questions
Angle between two lines in 2D 3 Answers
Calculate Position of Rigidbody after applying Force 0 Answers
Help with strings and decimals 1 Answer
Calculating triangle side length based on hypotenuse? 1 Answer
Draw a Vector line ??? 3 Answers