How can I calculate the size of one line in a Text component? For example the height of the line “HOW DO I CALCULATE” or “HEIGHT” in the picture below.
I have tried using the line height and line spacing properties but that does not give me the correct answer.
var lineHeight = textComponent.font.lineHeight * textComponent.lineSpacing;
The answer is fairly straightforward:
private float CalculateLineHeight(Text text)
{
var extents = text.cachedTextGenerator.rectExtents.size * 0.5f;
var lineHeight = text.cachedTextGeneratorForLayout.GetPreferredHeight("A", text.GetGenerationSettings(extents));
return lineHeight;
}
This one work with other ui scale mode:
public static float CalculateLineHeight (Text text) {
var extents = text.cachedTextGenerator.rectExtents.size * 0.5f;
var setting = text.GetGenerationSettings(extents);
var lineHeight = text.cachedTextGeneratorForLayout.GetPreferredHeight("A", setting);
return lineHeight * text.lineSpacing / setting.scaleFactor;
}