# Clipping text in algorithm

I'm looking for an algorithm that can clip a text ( GUI Mesh text ) when it's length is over 20 characters. The problem is I have one background image(textured plane) (orthographic), where I want to put a Gui mesh text on it, it happens that sometimes the text is larger in length than the supposed background image, and it gets out of the background mesh boundary.

Is it possible to fit that gui mesh into the boundary of the background plane mesh ?

• Why not just pass a maximum of 20 characters in? – Eejin Jul 30 '14 at 17:23
• @Eejin tried, but the testers just put some hebrew, chineese charachters which the glyph is so big and it passed the boundary :S – andreahmed Jul 30 '14 at 21:22
• If you're using unicode, you mostly have to accept that certain strings might break your rendering. Even many browsers and text layout libraries don't get everything exactly right. The problems don't stop at rendering either. Things like surrogate pairs and combining characters make comparing and modifying unicode strings significantly less pleasant. How much you want to support unicode and other locales is up to you, but few games do it very well. – bcrist Jul 31 '14 at 7:40

Now I am no unity expert (I opened it once two years ago), but I hope this helps.

First you would want to find the width of the string (in pixels). Something along these lines:

var textDimensions = GUI.skin.label.CalcSize(new GUIContent("text"));
//textDimensions.x contains the width in pixels.


Now you are going to want to see if that fits. If it doesn't then we are going to have to truncate the input string. (pseudo C++/C# mix)

var textDimensions = GUI.skin.label.CalcSize(new GUIContent(thestring));

while (textDimensions.x > somesize) {
cut off one character
textDimensions = GUI.skin.label.CalcSize(new GUIContent(truncated string));
}