Search Unity

Automatically Resizing TMP_InputField?

Discussion in 'UGUI & TextMesh Pro' started by col000r, Apr 1, 2019.

  1. col000r

    col000r

    Joined:
    Mar 27, 2008
    Posts:
    699
    I have a TMP_InputField on a Canvas and I want to change its size with the amount of space the InputField requires.

    My first attempt was to add a Listener to onValueChanged, then go through m_InputField.textComponent.textInfo.lineInfo and sum up lineHeight + lineSpacing, then apply that value to canvas.SetSizeWithCurrentAnchors( Axis.Vertical, height );

    But it's not working as intended. If I hit return and add a new line, that does not add a new line to the textInfo.lineInfo array, so the canvas doesn't get resized and the top line scrolls half-way out of view. If I type a letter it finally gets resized, but the top line stays halfway out of view :/

    Is there a better (correct?) way to do this?
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    See the following post and required changes to support Canvas Layout Components such as a ContentSizeFitter.

    You will need to add layout component to the TMP_InputField and child objects. Be sure to carefully make those changes as shown in that post.