Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by zmar0519 · May 03, 2011 at 08:47 PM · stringmathscrollviewlinecalculate

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Peter G · May 03, 2011 at 09:59 PM 0
Share

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"

avatar image Peter G · May 03, 2011 at 10:00 PM 0
Share

But interesting idea so +1.

avatar image Uzquiano · May 03, 2011 at 10:05 PM 0
Share

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...

avatar image Uzquiano · May 03, 2011 at 10:06 PM 0
Share

Sorry, but the comments do keep the EOL... but you could try

avatar image Owen-Reynolds · May 03, 2011 at 11:37 PM 0
Share

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.)

Show more comments
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges