x


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.

more ▼

asked Nov 17 '11 at 03:53 PM

hatzalex gravatar image

hatzalex
61 7 9 10

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...

Nov 17 '11 at 04:16 PM cj_coimbra

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 = Matrix4x4.TRS (Vector3(0, 0, 0), Quaternion.identity, Vector3 (Screen.height / nativeVerticalResolution, Screen.height / nativeVerticalResolution, 1));

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,skinHolder_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,skinHolder_Bad);

}

Nov 18 '11 at 08:31 AM hatzalex
(comments are locked)
10|3000 characters needed characters left

1 answer: sort oldest

Have you tried GUIStyle.wordWrap?

more ▼

answered Nov 18 '11 at 02:41 AM

Mox gravatar image

Mox
280 1 4

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.

Nov 18 '11 at 08:27 AM hatzalex
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2000
x553
x241
x157
x81

asked: Nov 17 '11 at 03:53 PM

Seen: 1013 times

Last Updated: Nov 18 '11 at 08:48 AM