I created a game object which acts as a repeating item for a UIGrid which I populate dynamically. The gameobject (RowItem) has couple of UILabel whose text can change on runtime depending on the content. The content of these UILabels overlap when the text is bigger. Can anybody help me in how to make UILabel expand relative to the adjacent UILabel when the text is more/less?
Asked
Active
Viewed 2,211 times
2 Answers
0
You can use transform.localScale
property of the UILabel
's property to scale it. Just make them bigger when the text is bigger than let's say 20 characters. Try with arbitrary values.
Also when you change the scale, run a re-align method, which aligns other labels so that they don't overlap.

0xC0DED00D
- 19,522
- 20
- 117
- 184
-
Try it out yourself first. If you find any trouble I'll help out. Refer this question for SO guidelines about asking code samples - http://meta.stackexchange.com/questions/167597/pure-code-asking-question-how-should-these-be-flagged – 0xC0DED00D Mar 22 '13 at 10:57
0
you can get the text length in pixel by this:
UILabel label;
float width = label.relativeSize.x * label.transform.localScale.x;
float height = label.relativeSize.y * label.transform.localScale.y;
Let's say that you want to set you max length to 100, you can do this:
if (width > 100)
{
label.localScale = new Vector3(100 / label.relativeSize.x, 100/ label.relativeSize.x, 1);
}
the second param for Vector3 is also based on relativeSize.x is not a typo, that makes sure your text will not become thin.
Hope this works.

Nicolas Dai
- 296
- 1
- 8