1

So in my script I want to know how many lines are displayed in a Textmeshpro - text (UI).

public TextMeshProUGUI commentaryText;

Debug.Log("number of lines " + (commentaryText.text.Split('\n').Length - 1));

I tried something like above but it will only show the number of lines with a \n newline character, however I also want to know about lines that are caused by wraparound when they reach the limit of the textbox. Is there a way to get the number of lines that the user sees?

Any advice appreciated. Thanks

enter image description here

user8810083
  • 599
  • 1
  • 5
  • 21

1 Answers1

3

Basically you can access commentaryText.textInfo.lineCount to get the number of lines. What I did is that on a button click it logs the number of lines for a TextMeshPro I created, so I was able to test it:

public TextMeshProUGUI textMeshPro;

public void Click()
{
    Debug.Log(textMeshPro.textInfo.lineCount);
}

Update for what you'd like from the comments:

maskable text mesh pro

You need to set the TMP to align to bottom and set the overflow to masking. Then you need a parent that has a mask component (I used the basic panel). The text that would go outside the size of the parent is hidden.

I'm not sure what you'd like with the line count, but this will count the hidden lines as well. So in my example if you use the lineCount it will log 6.

Ma Chrom
  • 116
  • 1
  • 5
  • Is it possible to get the number of lines regardless of whether they are being shown to the user? So effectively the number of lines that would show if we had overflow set to overflow. But in my case I don't want to have it on overflow, just know how many lines there actually are – user8810083 Sep 08 '21 at 11:19
  • the other issue im having is with the overflow mode. when i set it to truncate it truncates the bottom lines, im also looking for an overflow mode that instead truncates the top lines – user8810083 Sep 08 '21 at 11:27
  • I updated my answer based on what you asked about truncating the top lines, I'm not sure if I got what you'd like with the line number – Ma Chrom Sep 08 '21 at 12:28
  • thanks for your help, much appreciated. there is just 1 final thing that's not quite working and that is that my mask panel has to have an alpha of at least 1 to work. i basically dont want the mask image to display at all so was trying to set it to alpha of zero. i added the mask panel object to my OP. Can you see how to change it so that the mask image itself is not drawn, but the mask still acts on the text? – user8810083 Sep 08 '21 at 13:20
  • Under the mask component there's that checkbox "Show Mask Graphic", if you uncheck it the mask image should be hidden. – Ma Chrom Sep 08 '21 at 13:23