Text height returning as multiple lines

So, I’m trying to get the height of a text component and work with that height to calculate the size and position of other objects, all of this in the same frame, but for some reason the text height is returning a value as if the text were made of multiple lines. The longer the text(one line), the bigger the height. When all the calculation are done and the frame is created, then the text object is updated with its real height, which is the height of only one long line.

Here is a sample code, create a button and call CreateText(), the first time it will create the line and print its height(Multiple lines). Press it again and it will print the real height of the component.

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

public class TextSize : MonoBehaviour
{
    public GameObject canvas;
    private GameObject lineObject;
    private bool notCreated = true;

    public void CreateText()
    {
        if(notCreated)
        {
            DrawLine(1000, canvas);
            notCreated = false;
            print(lineObject.GetComponent<TMP_Text>().preferredHeight);
        }
        else
        {
            print(lineObject.GetComponent<TMP_Text>().preferredHeight);
        }
    }

    private GameObject DrawLine(float lineSize, GameObject parentPanel)
    {
        string lineText = "";
        //Create text object
        GameObject line = CreateTextObject(parentPanel);

        while(true)
        {
            //Fill lineSize with "_"
            lineText = lineText + "_";
            line.GetComponent<TMP_Text>().text = lineText;
            float lineTextWidth = line.GetComponent<TMP_Text>().preferredWidth;
            if(lineTextWidth > lineSize)
            {
                break;
            }
        }
        return line;
    }

    private GameObject CreateTextObject(GameObject parent)
    {
        GameObject text = new GameObject();

        text.AddComponent<CanvasRenderer>();
        text.AddComponent<RectTransform>();
        //add and set ContentSizeFitter
        text.AddComponent<ContentSizeFitter>();
        text.GetComponent<ContentSizeFitter>().horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
        text.GetComponent<ContentSizeFitter>().verticalFit = ContentSizeFitter.FitMode.PreferredSize;
        //add TMP_Text
        text.AddComponent<TextMeshProUGUI>();
        text.GetComponent<TMP_Text>().useMaxVisibleDescender = false;

        text.transform.SetParent(parent.transform, true);
        lineObject = text;

        return text;
    }
}

How can i fix this? is there like a way to update the UI manually so i can get the right height of the text.

Nevermind, i just found out that when disabling text wrapping, it will return only one line height.