1. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  2. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  3. We’re giving 2017.1 beta testers a chance to win t-shirts and a Nintendo Switch. Read more on the blog.
    Dismiss Notice
  4. Unity 2017.1 beta is now available for download.
    Dismiss Notice
  5. Unity 5.6 is now released.
    Dismiss Notice
  6. Get further faster with the Unity Plus Accelerator Pack, free for new Unity Plus subscribers for a limited time. Click here for more details.
    Dismiss Notice
  7. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  8. Learn how you'll soon be able to publish your games to China in four simple steps with Xiaomi. Sign up now for early access.
    Dismiss Notice

GUIText width and height

Discussion in 'iOS and tvOS' started by Poita_, Oct 1, 2009.

  1. Poita_

    Poita_

    Joined:
    Dec 18, 2008
    Posts:
    146
    I'm using GUIText for all our on screen text, and I was wondering a few things:

    1. Is there any way to make the text wrap at a certain width?

    2. Is there any way to find the width and height of the area used by the text.

    3. Is there any way to access the mesh of the text? (It creates a quad for each letter, right?)

    Generally, is any way to find out anything about the physical layout of the characters on screen.

    Thanks in advance.
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,769
    GUIText has a GetScreenRect method that returns the size of the box onscreen. I don't think you can set it to wrap or get a mesh.
     
  3. jtbentley

    jtbentley

    Joined:
    Jun 30, 2009
    Posts:
    1,376
    I'm not sure where I found this, but this is a slightly modified version of a wordwrapper I found somewhere.

    I thought I found it on the unify wiki, but I was unable to find it.. So I dug it up for you :)

    Code (csharp):
    1. var lineLength = 400; // Maximum width in pixels before it'll wrap
    2.  
    3. private var words : String[];
    4. private var wordsList : ArrayList;
    5. private var result = "";
    6. private var TextSize: Rect;
    7. private var numberOfLines = 1;
    8.  
    9. function cleanUp() // Easy to call from an external button
    10. {
    11.     FormatString(guiText.text);    
    12. }
    13.  
    14. function FormatString ( text : String ) {
    15.  
    16.     words = text.Split(" "[0]); //Split the string into seperate words
    17.     result = "";
    18.  
    19.     for( var index = 0; index < words.length; index++) {
    20.  
    21.        var word = words[index].Trim();
    22.    
    23.        if (index == 0) {
    24.           result = words[0];
    25.           guiText.text = result;
    26.        }
    27.  
    28.        if (index > 0 ) {
    29.  
    30.          result += " " + word;
    31.  
    32.           guiText.text = result;
    33.    }
    34.  
    35.    TextSize = guiText.GetScreenRect();
    36.    
    37.       if (TextSize.width > lineLength)
    38.       {
    39.           //remover
    40.           result = result.Substring(0,result.Length-(word.Length));
    41.        
    42.           result += "\n" + word;
    43.           numberOfLines += 1;
    44.           guiText.text = result;
    45.       }
    46.     }
    47. }
    Hope that helps :)
     
    gabicuesta73 likes this.
  4. Poita_

    Poita_

    Joined:
    Dec 18, 2008
    Posts:
    146
    Urgh, can't believe I didn't see that. I knew it existed for GUITexture, but for some reason it didn't cross my mind that it would exist for GUIText as well.

    Thanks for the code JTBentley, although it looks like a bit of a Schlemiel the painter's algorithm to me :p
     
  5. jtbentley

    jtbentley

    Joined:
    Jun 30, 2009
    Posts:
    1,376
    Well, like I said, its not actually my code :)

    Feel free to improve upon it and post it here :p
     
  6. Poita_

    Poita_

    Joined:
    Dec 18, 2008
    Posts:
    146
    Well yeah, it gets the job done, but I'll probably end up rolling my own as I need to handle hyphens and other things anyway.
     
  7. jtbentley

    jtbentley

    Joined:
    Jun 30, 2009
    Posts:
    1,376
    A place to start is better than nothing :)
     
  8. bibbinator

    bibbinator

    Unity Technologies

    Joined:
    Nov 20, 2009
    Posts:
    507
    Hi JT,
    I came across your code snippet here and used it as a base for a simple guitext formatter. In case somebody stumbles across this thread here's my C# version, reasonably optimized.

    Code (csharp):
    1.     public static Rect FormatGuiTextArea(GUIText guiText, float maxAreaWidth)
    2.     {
    3.         string[] words = guiText.text.Split(' ');
    4.         string result = "";
    5.         Rect textArea = new Rect();
    6.        
    7.         for(int i = 0; i < words.Length; i++)
    8.         {
    9.             // set the gui text to the current string including new word
    10.             guiText.text = (result + words[i] + " ");
    11.             // measure it
    12.             textArea = guiText.GetScreenRect();
    13.             // if it didn't fit, put word onto next line, otherwise keep it
    14.             if(textArea.width > maxAreaWidth)
    15.             {
    16.                 result += ("\n" + words[i] + " ");
    17.             }
    18.             else
    19.             {
    20.                 result = guiText.text;
    21.             }
    22.         }
    23.         return textArea;
    24.     }
    25.  
     
    gabicuesta73 likes this.
  9. gauthi24

    gauthi24

    Joined:
    Feb 4, 2012
    Posts:
    34
    This was exactly what I needed; thank you for the post. :D
     
  10. slampants

    slampants

    Joined:
    Oct 27, 2013
    Posts:
    4
    Super helpful, even this many years later. Thank you!
     
  11. gabicuesta73

    gabicuesta73

    Joined:
    Dec 8, 2014
    Posts:
    1
    Many thanks, I was programming a script to this but now I see that there are one working :)