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 yorickke · Jul 04, 2012 at 12:38 PM · textlabelheightwidth

height and width of label/box depending on amount of text

Hey there!

Perhaps I'm not using the right search terms, but I can't seem to find the answer to this question.

I would like to have the height and width of a label/box be flexible with the amount of text. So for example, if the string contains nothing I want the width and height to be 0. But when the string contains 30 characters I want the box the be large enough to fit the text and background of the label nicely and when the width reaches a specified amount I want the text to go to a next row.

I am wondering if there is an easy way to do this, I can't seem to find it. Thanks a lot for your help.

York.

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

2 Replies

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

Answer by delstrega · Jul 04, 2012 at 07:10 PM

Just use GUILayoutUtility.GetRect(). It returns a Rect that's big enough to hold your content - be it text, images or both. The nice thing is, that it doesn't actually draw anything so you can modify that rect if you wish before drawing.

Use it like this:

 Rect labelRect = GUILayoutUtility.GetRect(new GUIContent("This is some text"), "label");

Now you have a nice rect which you can use directly to actually draw the label OR first do other stuff (like going to the next row and such).

Drawing the label is as easy as

 GUI.Label(labelRect, "This is some text");


That should give you a starting point.

Comment
Add comment · Show 3 · 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 yorickke · Jul 05, 2012 at 12:50 PM 0
Share

That was exactly what I was looking for, thanks a lot :)

avatar image fendorio · Apr 09, 2014 at 01:58 AM 0
Share

Silly question, if there is such a thing. But could you point out what overload of:

GUILayoutUtility.GetRect(new GUIContent("This is some text"), "label")

is?

avatar image oferei · May 22, 2015 at 11:34 AM 0
Share

I suggest adding the ExpandWidth(false) option to prevent allocation of extra space:

 rect = GUILayoutUtility.GetRect(GUIContent(text), 'label', GUILayout.ExpandWidth(false))
avatar image
3

Answer by erf23 · Apr 28, 2014 at 01:57 AM

This is an old thread, but while I was trying to do the same thing, I found that a better answer is to use GUIStyle.CalcMinMaxWidth. GetRect reserves layout space, but CalcMinMaxWidth does not.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Generating Text with a script - setting width/height? 0 Answers

moving an object at the edge of plane? 1 Answer

Get height and width of gameobject set with anchors 0 Answers

NGUI - How to disable a UILabel ? 1 Answer

Same button position in all Android devices 2 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