- Home /
How can I calculate the height of one line in a Text component?
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;
Answer by Spankenstein · Jul 16, 2017 at 07:10 PM
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;
         }
Small tip, to take your text's line spacing into account replace your return statement with:
 return lineHeight * text.lineSpacing;
Answer by M_oenen · Oct 12, 2020 at 08:32 AM
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;
 }
Your answer
 
 
             Follow this Question
Related Questions
Figure out the pixel size of a character 1 Answer
UI.Text font size is somehow resized >_< 0 Answers
Re-sizing UI text font relative to screen size 3 Answers
How do I calculate how big text is in a world space canvas UI Text? 2 Answers
How to get the current "best fit" size of a Text component? 3 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                