Search Unity

Text Clipping with dots

Discussion in 'Immediate Mode GUI (IMGUI)' started by Racer777, Sep 18, 2013.

  1. Racer777

    Racer777

    Joined:
    Sep 18, 2013
    Posts:
    1
    Hi Everyone!

    I searched very long time on internet but could not find a solution.
    my problem:
    I know there is text clipping in GUIStyle.. but this hides only the rest of the text when it comes to the edge. Me, howerver, need to shorten the text with three dots like "..." when it is too long and doesnt fit my label or button. How can i achieve that?? Is there any workarounds?
    Every suggestions, ideas and thoughts are appreciated!
     
  2. Nition

    Nition

    Joined:
    Jul 4, 2012
    Posts:
    781
    There's no built in support for ellipses in Unity unfortunately. It'd certainly be possible to add, but I started writing some ellipsis code a while ago and decided against it because it was going to need to do an inordinate amount of processing to work out where to cut off the text and add the dots.

    Unity provides handy methods to find out how much space text will take up (CalcSize, CalcHeight), so it's easy to work out if text will clip. But it doesn't expose anything to work out how much text will fit in a given area, or how much text will remain after clipping. So to find out where to cut off the text and add an ellipsis you'd have to try CalcSize seveal times to basically search for the right amount that'd fit. On top of all the working out widths etc already I decided it was too much to bother with.

    You'd want to start with something like this (untested):

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. // Clipped text gets an ellipsis
    5. public static class EllipsisText {
    6.  
    7.     const string ellipsis = "...";
    8.  
    9.     public static string Get(string text, Rect area, GUIStyle style) {
    10.         // Early exit: If text isn't clipped at all, it doesn't need an ellipsis
    11.         if (style.clipping == TextClipping.Overflow) return text;
    12.  
    13.         bool willClip = false;
    14.         if (style.wordWrap) { // Can be multi-line
    15.             GUIContent textContent = new GUIContent(text);
    16.             float textHeight = style.CalcHeight(textContent, area.width);
    17.             if (textHeight > area.height) willClip = true;
    18.         }
    19.         else { // Single line
    20.             GUIContent textContent = new GUIContent(text);
    21.             Vector2 textSize = style.CalcSize(textContent);
    22.             if (textSize.x > area.width) willClip = true;
    23.         }
    24.  
    25.         if (willClip) {
    26.             GUIContent ellipsisContent = new GUIContent(ellipsis);
    27.             float ellipsisWidth = style.CalcSize(ellipsisContent).x; // Ellipsis size with this style
    28.             return FindFittableText({some parameters here});
    29.         }
    30.         return text;
    31.     }
    32.  
    33.     // Unity has methods to find how much space text will take up, but nothing to find out
    34.     // how much text will fit in a space, or how much text is left after clipping.
    35.     // So we have to make some informed guesses to get there
    36.     string FindFittableText({some parameters here}) {
    37.         // This is where you'd try adjusting the text length and checking CalcSize until it just fit within area
    38.         // A good starting guess would be e.g. if the area the text needed was 200% of the area available,
    39.         // try reducing the text length by 50%.
    40.     }
    41. }
    42.  
    Then when you wanted text, instead of entering the text directly you'd call the method e.g.

    Code (csharp):
    1.  
    2. Rect textArea = new Rect(xPos + 10, yPos + mapSize - 10, mapSize - 24, textHeight);
    3. GUI.Label(textArea, EllipsisText.Get("My text", textArea, TextStyle), TextStyle);
    4.