Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Label Autosize

Discussion in 'Immediate Mode GUI (IMGUI)' started by SteveJ, Mar 21, 2011.

  1. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,066
    Given the following code:

    Code (csharp):
    1.  
    2. scrollViewVector = GUI.BeginScrollView(Rect(25, 25, 430, 220), scrollViewVector, Rect(0, 0, 400, ???));
    3. GUI.Label(Rect(0, 0, 400, ???), "Whatever text I want here which could be short or VERY long...", thisStyle);
    4. GUI.EndScrollView();
    5.  
    Is it possible to make the label auto-size itself to however high it needs to be (???) for the text it's displaying?
     
  2. mattcscz

    mattcscz

    Joined:
    Mar 7, 2010
    Posts:
    411
    i think the only way to scale it would be through GUI.skin
     
  3. spinaljack

    spinaljack

    Joined:
    Mar 18, 2010
    Posts:
    984
    probably need to use something like string.length to find out how long the text is
     
  4. jayw

    jayw

    Joined:
    Aug 8, 2012
    Posts:
    7
  5. Loius

    Loius

    Joined:
    Aug 16, 2012
    Posts:
    546
    GUIStyle.CalcSize will get you the exact space needed by an element without rendering it.
     
  6. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    Agree, GUIStyle.CalcSize is the answer. Just a tip: you should keep the label invisible for the first frame (until you measure it), because it might give you a noticeable flicker (visible size change).