- Home /
Get The size of the Text in unity UI
Hello i am using unity 5.4
I have a image having a child Text. i want to scale the image based on the string size given to the text. i am using following code to get the work done.
m_textBgImage = Instantiate (m_textBgPrefabs); // This is a image prefab has text as its child
m_chatText = m_textBgImage.GetComponentInChildren<Text> ();
m_chatText.text = m_textToDisplay;
Vector2 widthHeight = GetSizeOfWord (m_textToDisplay);
textBgImage.GetComponent<RectTransform>().sizeDelta = widthHeight;
Vector2 GetSizeOfWord(string word)
{
float width = 0.0f;
float biggestWidth = 1.0f;
float biggestHeight = 0.0f;
int spaceCounts = 0;
print ("m_chatText.fontSize : "+m_chatText.fontSize);
foreach (char c in word)
{
CharacterInfo charInfo;
if (c == ' ')
{
spaceCounts++;
}
else
{
m_chatText.font.GetCharacterInfo (c, out charInfo, m_chatText.fontSize);
width += charInfo.glyphWidth;
if (biggestWidth < charInfo.glyphWidth) {
biggestWidth = charInfo.glyphWidth;
}
if (biggestHeight < charInfo.glyphHeight) {
biggestHeight = charInfo.glyphHeight;
}
print ("Char : "+c);
print ("Advance : "+charInfo.advance);
print ("Height : "+charInfo.glyphHeight);
print ("Width : "+charInfo.glyphWidth);
print ("Index : "+charInfo.index);
print ("Bearing : "+charInfo.bearing);
print ("MaxX : "+charInfo.maxX);
print ("MaxY : "+charInfo.maxY);
print ("Size : "+charInfo.size);
print ("Style : "+charInfo.style);
print ("uvBottomLeft : "+charInfo.uvBottomLeft);
print ("uvBottomRight : "+charInfo.uvBottomRight);
print ("uvTopLeft : "+charInfo.uvTopLeft);
print ("uvTopRight : "+charInfo.uvTopRight);
}
}
print ("Space count : "+spaceCounts);
float textWidth = width + (biggestWidth * spaceCounts);
float textHeight = Mathf.Floor (textWidth / m_maxLengthOfText);
textWidth = Mathf.Min( m_maxLengthOfText,width);
print ("W : " + textWidth + " H : " + textHeight * biggestHeight);
return new Vector2(textWidth, textHeight * biggestHeight);
}
The "out charInfo" is most of the time outputting zero value and all the print statements displays zero, and randomly once in a blue moon it gets the correct value, the height and width of the character.
Does anyone know why i am getting zero values , where i am missing out?
Answer by DiegoJQB9 · Aug 05, 2016 at 02:04 PM
I guess you mean the font size?
For UnityEngine.UI.Text objects you can get info of the displayed text with the cachedTextGenerator and cachedTextGeneratorForLayout properties.
Hey Diego , Thanks for your time, Unfortunately Text generator is not what i am looking for, and also i meant length of the string in pixels in canvas space not the font size. I had to tweak the above code to get it work correctly
Answer by rjr · Aug 08, 2016 at 05:17 AM
Solved!!!
I had kept m_textBgImage prefab width and height to zero in the inspector, I changed it to width =1 and Height = 1
And after instantiating m_textBgImage i gave a small delay before calling GetSizeOfWord(string word) ie. i put GetSizeOfWord(string word) in a co-routine and waited for 0.1 seconds. and it seems to be calculating proper pixel values for each characters.
I suppose after instantiation unity takes some time to initialise the Text property such as its font, material info which is accessed in GetCharacterInfo method, So if it is accessed before initialisation it will return zeros