Text Mesh Pro detect automatic word wrap line break in script

Hi there,

in Text Mesh Pro line breaking works automatically, wich is great! When a string is to long for a text box a line break is created. My question is this: Is there a way to know in script wheather this occured or not? Or respectively how many lines a string takes up in a given text box?

This would really help, thanks!

Hi,

My idea was to split sentence to lines (
). Next was split to words and adding word by word to TextMeshProUGUI component.
After that i had to check if preferredHeight is bigger or not.
It was not enough because sometimes we have a lot of letters in one line so i also had to face with that problem.

Probably it’s not super optimized solution. Also it can’t handle when we have spaces before and after word. I used Trim function so it is purposed solution. I didn’t need that feature so i just skip that.
Don’t work with margins, <.b> e.t.c.

using UnityEngine;
using TMPro;
using System.Collections.Generic;

[RequireComponent(typeof(TextMeshProUGUI))]
public class SplitLines : MonoBehaviour
{
    TextMeshProUGUI container;
    [SerializeField] List<string> resultsList;
    
    float lineHeight;
    float currentPreferredHeighLimit;

    string currentLineText;

    private void Awake() 
    {
        container = GetComponent<TextMeshProUGUI>();
    }

    private void OnEnable() 
    {
        Process();
    }

    void InitValues()
    {
        resultsList = new List<string>();
        currentPreferredHeighLimit = 0;
        currentLineText = "";

        container.enabled = true;
        startValue = container.text;
        container.enableAutoSizing = false;

        lineHeight = GetLineHeightWithSpace();
    }

    private void Process()
    {
        InitValues();

        string trimed = container.text.Trim();
        trimed = trimed.Replace("\

", "
“);
string lines = trimed.Split(”
");
container.text = “”;

        currentPreferredHeighLimit += lineHeight;

        foreach(var line in lines)
        {
            ProcessLine(line.Trim());
        }
    }

    private void ProcessLine(string line)
    {
        currentLineText = "";

        string[] words = line.Split(' ');

        foreach(var word in words)
        {
            if (word.Replace(" ","").Length >= 1)
            {
                ProcessWord(word.Trim());
            }
        }

        currentPreferredHeighLimit += lineHeight;
        resultsList.Add(currentLineText);
        container.text += "

";
}

    private void ProcessWord(string word)
    {
        string space = currentLineText.Length > 1 ? " " : "";

        container.text += space + word;

        if (container.preferredHeight > currentPreferredHeighLimit)
        {
            if (currentLineText.Length < 1)
            {
                ProcessLetters(word);
            }
            else
            {
                resultsList.Add(currentLineText);
                currentLineText = "";
                currentPreferredHeighLimit += lineHeight;

                container.text = container.text.Remove(container.text.Length - word.Length);
                container.text += "

";
ProcessWord(word);
}
}
else
{
currentLineText += space + word;
}
}
private void ProcessLetters(string word)
{
container.text = container.text.Remove(container.text.Length - word.Length);

        char[] chars = word.ToCharArray();

        foreach(var c in chars)
        {
            container.text += c;

            if (container.preferredHeight > currentPreferredHeighLimit)
            {
                resultsList.Add(currentLineText);
                container.text = container.text.Remove(container.text.Length - 1);
                container.text += "

";
currentLineText = “”;
currentPreferredHeighLimit += lineHeight;

                container.text += c;
            }

            currentLineText += c;
        }

        string tmp = currentLineText;
        currentLineText = "";
        container.text = container.text.Remove(container.text.Length - tmp.Length);
        ProcessWord(tmp);
    }

    private float GetLineHeightWithSpace()
    {
        string text = container.text;
        container.text = "|";
        float valueFirstLine = container.preferredHeight;
        container.text += "

|";
float valueTwoLines = container.preferredHeight;
container.text = text;
return Math.Max(valueFirstLine , valueTwoLines - valueFirstLine);
}

}