Search Unity

Question How to get Display Width of Tab Character "\t"

Discussion in 'Immediate Mode GUI (IMGUI)' started by darkLink_32, Feb 2, 2023.

  1. darkLink_32

    darkLink_32

    Joined:
    Apr 22, 2018
    Posts:
    5
    Hello,

    I would like to know if there is way to get the display width of the tab character "\t" in unity, or in other words, the length of the tab stop currently being used.

    Right now I am using GUI.skin.GetStyle to achieve this, but it doesn't appear to be accurate. To confirm this, I used the code below:

    Code (CSharp):
    1.  
    2. private void OnGUI()
    3.         {
    4.             _widthOfStringWithTab = GUI.skin.GetStyle("label").CalcSize(new GUIContent("a"+"\t"+"aa")).x;
    5.             _widthOfString = GUI.skin.GetStyle("label").CalcSize(new GUIContent("aaaaaaaaaaa")).x;
    6.  
    7.             Debug.Log("TAB = " + _widthOfStringWithTab + "STRING = " + _widthOfString);
    8.         }
    9.  
    This outputs the following lengths:

    _widthOfStringWithTab = 78
    _widthOfString = 77

    So I would think that if I display a label with the first string, it would look longer than the second right?

    Well the string "a"+"\t"+"aa" appears as:

    upload_2023-2-2_10-12-45.png

    and "aaaaaaaaaaa" appears as:

    upload_2023-2-2_10-14-36.png

    So as you can see, the length of the tab character is not being calculated accurately.

    Is this not possible?

    Thank you,
     
  2. darkLink_32

    darkLink_32

    Joined:
    Apr 22, 2018
    Posts:
    5
    So to provide an update:

    I decided to start using Text Mesh Pro which helped to bypass this issue. I ended up using Rich Text alignment tags to get the tabular look I was looking for. Also it looks like you can define your own tab value in the font file you're using with TMP so this may help with alternative solutions.