Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

TextMesh Pro TMP_InputField is there any way to limit the number of rows entered?

Discussion in 'UGUI & TextMesh Pro' started by SniperED007, Jan 21, 2019.

  1. SniperED007

    SniperED007

    Joined:
    Sep 29, 2013
    Posts:
    345
    I have a Input Field with Multi Line support and character limit of 20.
    But I don't want the user to be able to enter more than 3 lines of text.

    I've tried a couple of hacks like this on the OnValueChanged:

    foreach (var item in InputField.textComponent.textInfo.characterInfo)
    {
    if (item.lineNumber > 2)
    {
    InputField.text = InputField.text.Remove(item.index, InputField.text.Length - item.index);
    break;
    }
    }

    but wondering if there isn't a better way?
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Until I add a better way of handling this, here is a suggestion on a slight improvements on this.

    Instead of checking each character, first check textInfo.lineCount and if the line count is greater than your limit, check textInfo.lineInfo[limit - 1].lastCharacter. Limit - 1 because line 1 is at array index 0.

    This will give you the index of the last character on the valid line after which you can truncate the line of text.
     
  3. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Quick update: I just added support for limiting number of lines when using Line Type - Multiline Newline or Submit.

    Should have a new release of TMP this week. Once available, please give it a try and let me know if it works as expected.
     
  4. SniperED007

    SniperED007

    Joined:
    Sep 29, 2013
    Posts:
    345
    Thanks, that worked great.
    And thanks for the quick response too.