- Home /
How exactly "\t" works?
"\t" is used to add a Tab in text display. But what is the underlying theory of how it works? Sometimes have to manually add more space to actually achieve the real aligning effects.
For example:
GUI.Label(name[0]+" \t"+ life);
GUI.Label(name[1]+" \t"+ life);
GUI.Label(name[2]+" \t"+ life);
So I think understanding exactly how it works will be very useful.
Answer by Bunny83 · May 10, 2011 at 11:26 AM
There's no way to get a acceptable result with variable-width fonts. Tab actually does add a variable length but it has a maximum length. You got the same problem with monospaced fonts. Just imagine tab-stops every 8 characters (just vertical lines where to stop is you use a tab)
If you use this strings "Hello\tWorld" it will move to the next tabstop after Hello
//| | | | |
//|Hello World
// |->
But if your first string is equal or greater than a single tabstop(8 in our case) it will take the next one.
"Welcome:\tPlayerName"
//| | | | |
//|Welcome: PlayerName
// |------>
I wouldn't struggle with that :D. Just use two seperate labels. With two labels you can even allign the two parts differently. For example playername and points. It looks much better when the points are right-aligned.
var playerNames : String[];
var points : String[];
function OnGUI()
{
var rightAlignedLabel = new GUIStyle(GUI.skin.label);
rightAlignedLabel.alignment = TextAnchor.MiddleRight;
rightAlignedLabel.fixedWidth = 60;
GUILayout.BeginVertical(GUILayout.Width(300));
for (var i=0; i<playerNames.length; i++)
{
GUILayout.BeginHorizontal();
GUILayout.Label(playerNames[i]);
GUILayout.Label(points[i],rightAlignedLabel);
GUILayout.EndHorizontal();
}
GUILayout.EndVertical();
}
In this example i use a fix size of 60 for the points label. Thw whole area is set to 300 pixel width and can grow in height. If you want to test it make sure you set both arrays to an equal elementcount (i.e. 3x playerNames and 3x points)
Some related things: GUILayout, GUIStyle.alignment
This is the answer I am looking for! Excellent and thank you for your time. people come and vote this up~ lol
This was NOT exactly what I was looking for, but the answer is so good, it helped me figure out what was going with TabSize and what the docs for Text$$anonymous$$esh mean by a "multiplum".
Answer by CHPedersen · May 10, 2011 at 06:38 AM
The reason you're not getting a uniform alignment out of the tab character is that it doesn't advance the string until some common tabulator stop, after which it would add life
, which is what an old fashioned typewriter would do (and what you seem to expect, judging from your code). :)
The tab character is a whitespace character (ASCII 9) with a fixed width which corresponds to some multiple of the Space-character. Just how much this multiple is varies from system to system and programming language to programming language.
Consider that since the strings in name[0]
, name[1]
and name[2]
have different lengths, you're also going to get misaligned text on your GUI label if you add the same multiple of spaces (the tab character) to the string. The manual correction you did to achieve the alignment then simply corresponds to the difference in length between the strings in name[]
.
Hope that clears it up a bit.
Still not quite sure. You see, tab will actually add dynamic number of spaces automatically. If difference of the sizes of strings is too big, it will not work
That's not exactly how it works. Even back in CRT-monitor times and the good old textmode the tab always goes to the next tabstop. Nowadays it's even more complicated since we have variable-width-fonts. The function of the tab is still the same. It's not really bound to a character size. Normally a tab is a multiply of the space(ASCII 32) character. On my computer the tab size is 4. The variable width is the real problem. One "$$anonymous$$" is almost as large as 5 "i" and therefore they use the same space and results in the same tabstop.
Oh, and the tab character (ASCII 9) is not a viewable character. It's a control-character that is interpreted by the system that is viewing the text.
Answer by Dreamer · May 10, 2011 at 07:16 AM
I am now thinking is there a way to set a String to certain size? For example like
name[0].setSize(12) will be "Jack "
name[1].setSize(12) will be "Johnthan "
A way to uniform all string sizes. If have this String function, I think this will solve this aligning problem.
Hmm, maybe we could write by our own
function setSize(_s:String,_num:int):String{ var temp_s=_s; if(_s.length>=_num) return _s; else{ for(var i:int;i<_num-_s.length;i++){ temp_s+=" "; } return temp_s; } }
for(var i:int;i<name.length;i++){ GUI.Label(name[i].setSize(12)+" \t"+ life); }
No, follow Bunny83's advice. :) He's right. The above simply pads your strings with Space-characters until they contain the same amount of characters, which doesn't solve the problem unless your font is monospaced. Just as "iiiii " doesn't have the same length as "AAAA " right here, even though they contain the same amount of characters
Answer by whydoidoit · Apr 12, 2012 at 08:35 PM
You can get the result by using a combination of tabs and spaces, so long as you know how large Unity will make the base string. I've created a class that extends String with the ability to do this. There are times when it is necessary (when putting things in a Popup for instance).
You can find the fixed width string class for download here