- Home /
Fitting text into label
Hallo everyone,
Im currently working on a project where player can add their own text into the game. Im using GUI.labels to store the text. But if i write a pretty long word the word eventually goes offscreen. Is there a way to force the text to stay in the Label boundaries and automatically rescale itself to a smaller size when he reached the boundary? The project is for the iPhone. I know font scaling is prohibited on iPhone so maybe there is a workaround like a scrollbar or something?
Here is my code:
function OnGUI() {
GUI.matrix = Matrix4x4.TRS(new Vector3(GUIsF.x,GUIsF.y,0),Quaternion.identity,GUIsF);
GUI.Label(Rect(20,20,parchment.width/2,parchment.height/2),tempWord,mySkin_Good);
GUI.matrix = Matrix4x4.TRS(new Vector3(Screen.width - 258*GUIsF.x,GUIsF.y,0),Quaternion.identity,GUIsF);
GUI.Label(Rect(-20,20,parchment.width/2,parchment.height/2),currentWord,mySkin_Bad);
}
the tempWord & currentWord are the string which are being handled in another function. The matrix handles rescaling on different resolutions.
Thanks in advance.
Not sure if this would work in your case but you could check the string length and then use an appropriate GUIStyle. I mean, if your is string size is > X & < Y then use GUIStyle normal_sized_style, else if string size is > Y use GUIStyle small_sized_style...
Worked like a charm mate, here is the full code for the interested:
function OnGUI() {
if(currentWord.Length > 5)
{
var skinHolder_Good = mySkin_Good_small;
}else
{
skinHolder_Good = mySkin_Good;
}
if(currentWord.Length > 5)
{
var skinHolder_Bad = mySkin_Bad_small;
}else
{
skinHolder_Bad = mySkin_Bad;
}
GUI.matrix = $$anonymous$$atrix4x4.TRS (Vector3(0, 0, 0), Quaternion.identity, Vector3 (Screen.height / nativeVerticalResolution, Screen.height / nativeVerticalResolution, 1));
GUI.matrix = $$anonymous$$atrix4x4.TRS(new Vector3(GUIsF.x,GUIsF.y,0),Quaternion.identity,GUIsF);
GUI.Label(Rect(20,20,parchment.width/2,parchment.height/2),tempWord,skinHolder_Good);
GUI.matrix = $$anonymous$$atrix4x4.TRS(new Vector3(Screen.width - 258*GUIsF.x,GUIsF.y,0),Quaternion.identity,GUIsF);
GUI.Label(Rect(-20,20,parchment.width/2,parchment.height/2),currentWord,skinHolder_Bad);
}
Answer by Mox.du · Nov 18, 2011 at 02:41 AM
Have you tried GUIStyle.wordWrap?
I had not. Thanks for the tip, but there is still one problem. WordWrap seems to wrap the text only on the horizontal axis, so when a user adds a long word the text just jumps a line under and continues there.
Your answer
Follow this Question
Related Questions
Text is too small while displaying in device 1 Answer
Color.white not so much white... 3 Answers
Changing the size of a GUI label 1 Answer
Changing a GUILabel text SIZE 3 Answers
[Closed, I guess] Imported font appears very messy on UI Text 1 Answer