How To Force Text Bounds Within Parent (not canvas)

Hi all. This is my first time posting here and I’m a complete novice so please take it easy on me.

Now to the nitty gritty…

I’ve created a pseudo UI notepad in my game. I say pseudo because it’s not canvas based and exists in world space. The reason for this is because I also want the player to be able to draw in the notepad and, as we all know, the built-in LineRenderer doesn’t play nice with canvas elements

The LineRenderer works fine, but the problem is with typing text…

The way I handle text is by Raycasting onto the notepad and instantiating an empty TextMeshPro prefab. I update the text in real time by transferring it from an invisible InputField that I activate upon instantiation. I also use a content size fitter on the TextMeshPro prefab to have it’s bounds expand dynamically to fit the text as it updates from the InputField.

Now the problem is that I simply can’t figure out a way to force the TextMeshPro prefab to dynamically resize it’s bounds to fit within the transform of the parent (the notepad page transform)

I wonder if there’s a way to measure the distance from the mousePosition upon instantiation to the edge of the parent mesh and then set that to be the width of the instantiated TextMeshPro prefab…

I’ve included some images and code for added context. I apologize in advance if I haven’t explained things properly.

This is an image of the problem. The line drawing is fine, but the text goes off the page.

This is the Update code for handling the raycast onto the notepad.

private void Update()
{
    if (newNote != null)
    {
        newNote.text = inputField.text;
    }

    if (Input.GetMouseButtonDown(0))
    {
        if (EventSystem.current.IsPointerOverGameObject())
        {
            return;
        }

        Ray ray = equiprenderCam.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit) && hit.collider.gameObject.tag == "pages")
        {
            if (!isWriting)
            {
                WriteNote(hit);
                return;
            }
            else if (isWriting && newNote.text == "")
            {
                Destroy(newNote.gameObject);
                WriteNote(hit);
            }
            else
            {
                inputField.text = null;
                WriteNote(hit); 
            }
        }              
    }
}

This is the code for instantiating the note:

    private void WriteNote(RaycastHit hit)
{
    isWriting = true;
    Vector3 mousePos = hit.point;       
    newNote = Instantiate(notePrefab);
    newNote.transform.SetParent(hit.transform, false);
    newNote.transform.position = mousePos;
    inputField.ActivateInputField();
}

I think I figured it out…

I essentially converted the raycast hit.point to localspace on the quad and then created a triangle out of this point and the two vertices comprising the edge of the quad (page). Then I measured the distances of each side of the triangle and used that information to calculate the height of the triangle (the distance from the mouse click to the edge of the quad). Then I fed that number into the instantiated text’s sizeDelta.