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
3
Question by AndyF · Feb 07, 2011 at 05:22 PM · guifont

How to calculate the height of wordwrapped gui text

I am currently using a single font and altering it's size to put text into a GUI.Label. I use the following code to make sure that the text fits my label:

var customButton : GUIStyle; static var content = new GUIContent(); var string = "Contents of label"; var sizeX = 200; var sizeY = 40; var startPosX = 20; var startPosY = 20;

customButton.wordWrap = false; content = GUIContent(string); customButton.fontSize = 60; var boxContentSize : Vector2 = customButton.CalcSize(content); while (boxContentSize.y > sizeY || boxContentSize.x > sizeX) { customButton.fontSize -= 5; boxContentSize = customButton.CalcSize(content); }

GUI.Label (Rect (startPosX,startPosY,sizeX,sizeY), string, customButton);

This code reduces the font size until the text fits the box size. If I print out the values of boxContentSize.x and boxContentSize.y they seem reasonable values based on the assumption that they are in screen pixels.

As I sometimes want to put in longer strings and wrap the text I set wordWrap = true. However, when I do this, the boxContentSize.y is a very large value and does not seem to relate to the pixel size in the Y direction. I've tried using

customButton.CalcHeight(content, boxContentSize.x);

but this also gives a value in boxContentSize.y that is larger than expected. Can anyone explain what width value is supposed to be passed in CalcHeight and how I get a reasonable value for the height of the text based on a given font size? Any help would be appreciated.

Comment
Add comment · Show 2
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 Bunny83 · Feb 07, 2011 at 09:29 PM 0
Share

CalcHeight need to know the width of the area you want to insert the wordwrapped text to be able to calculate the height. In your case you need sizeX. customButton.CalcHeight(content, sizeX);

avatar image AndyF · Feb 08, 2011 at 10:20 AM 0
Share

Thanks. It seems I was just using the wrong width as the height is now a sensible value. I just changed the while loop to compare the height with sizeY and everything is fine.

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by yoyo · Feb 07, 2011 at 05:51 PM

You need both GUIStyle.CalcSize and GUIStyle.CalcHeight.

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

No one has followed this question yet.

Related Questions

GUIStyle.CalcSize giving wrong answer 0 Answers

Is there a way to measure the pixel with/height of a string with a given font? 2 Answers

Possible to change GUI.Label fontsize without using GUIStyle? 1 Answer

GUIBox fontSize without changing other labels? 3 Answers

Sometimes the GUI selectively doesn't render some letters 1 Answer


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