Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

TextMesh Pro isTextTruncated TextMeshPro

Discussion in 'UGUI & TextMesh Pro' started by dappledore, Mar 27, 2018.

  1. dappledore

    dappledore

    Joined:
    Feb 16, 2015
    Posts:
    12
    How does isTextTruncated actually work? For a string that is visually truncated, if i set the overflow mode to truncate it returns false. If i set the overflow mode to eclipse it returns true, is that by design?
    When is the isTextTruncated info available i have to wait a little before its available. I haven't found much info about this, so asking here.
     
  2. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    The reason why this property isn't behaving as you would expect is most likely due to when you are checking it.

    In order for this flag to be set, the text has to be generated (at least once). So if you were to set the text to some value and then check this property right away, it would return false. However, if just after setting the text, you forced a generation of the text by using ForceMeshUpdate() then isTextTruncated would return true assuming it indeed was truncated.

    This property is not currently serialized and maybe it should.

    P.S. Sorry that I didn't respond to your post earlier, I simply missed it between the other posts since it didn't have the "TextMesh Pro" green prefix on it which makes it easier for me to spot those TMP related threads :)
     
    EZaca likes this.
  3. dappledore

    dappledore

    Joined:
    Feb 16, 2015
    Posts:
    12
    I tried using ForceMeshUpdate() and it didn't change anything
     
  4. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    Make sure you are using Overflow mode Truncate or Ellipsis.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using TMPro;
    4. public class IsTextTruncatedCheck : MonoBehaviour {
    5.     private TMP_Text m_TextComponent;
    6.     private void Awake()
    7.     {
    8.         m_TextComponent = GetComponent<TMP_Text>();
    9.         m_TextComponent.ForceMeshUpdate();
    10.  
    11.         Debug.Log(m_TextComponent.isTextTruncated);
    12.     }
    13. }
    14.  
    The following script added to a text object should work as expected.

    P.S. The change to this property is already included in the latest release of TextMesh Pro for Unity 2018.1 which is available via the Package Manager.
     
    Last edited: Apr 2, 2018
  5. dappledore

    dappledore

    Joined:
    Feb 16, 2015
    Posts:
    12
    Yes as i mentioned i am using truncate mode, i am setting text dynamically not at design time would that change anything? So the fix was only recent?
     
  6. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    This should work with ForceMeshUpdate() as per the example above. The recent change makes it so that you don't have to use ForceMeshUpdate.

    If the state is isTruncated is wrong when you test, can you please provide an example script that reproduces the behavior?
     
  7. dappledore

    dappledore

    Joined:
    Feb 16, 2015
    Posts:
    12
    This is a function i use to put Ellipsis in the middle of text, apple style

        IEnumerator EllipsisMiddle(TextMeshProUGUI tmpText, int len) {
    tmpText.overflowMode = TextOverflowModes.Ellipsis;
    yield return new WaitForEndOfFrame ();
    if (!tmpText.isTextTruncated) yield break; //only eclipse mode seems to return if text is truncated
    tmpText.overflowMode = TextOverflowModes.Truncate;

    string text = tmpText.text;
    tmpText.text = text.Substring(0,len/2) + "…" + text.Substring(text.Length - (len - (len/2+2)));
    }
     
  8. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    Thank you for providing this sample script. Unfortunately, without knowing the specifics of this "tmpText" object that is being referenced, there are too many unknowns for me to reproduce the behavior.

    Any chance you can provide a Repro scene or a script that can be attached to a text object to reproduce what you are experiencing on your end?
     
  9. dappledore

    dappledore

    Joined:
    Feb 16, 2015
    Posts:
    12
    hello Stephan,
    Thanks for your help so far, i really wanted to know if that was by design or not and you said it wasnt . ATM it is working , although it was a little hacky so i will stick with that for now. I have finished this project for now, i will be visiting it again later.
     
    Stephan_B likes this.
  10. Ledrec

    Ledrec

    Joined:
    Sep 10, 2014
    Posts:
    9
    Yeah stephen isTruncated for me is not giving me true when its clearly truncated, and i havent found a way to fix that... im using unity 2017.3.1f1 and the latest release on the asset store... im really worried because i really need this to work
     
  11. Ledrec

    Ledrec

    Joined:
    Sep 10, 2014
    Posts:
    9
    I think the problem is that the text is not reallly truncated, it just looks that way... when i change anything on the editor the text updates and works as it should... but i need it to work when running.
     
  12. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    558
    ForceMeshUpdate solved my problem! :)
     
  13. Mark_Hong_IGG

    Mark_Hong_IGG

    Joined:
    Nov 13, 2018
    Posts:
    2
    After some experimenting I found that, Wrapping must be set to Enabled for isTextTruncated to work properly

    upload_2019-10-2_14-22-52.png

    and of course, if you are calling isTextTruncated right after changing the text, ForceMeshUpdate Should be called first.
     
  14. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    I made changes related to these Overflow modes in the latest preview releases which are version 1.5.0-preview.1 for Unity 2018.4 and 2.1.0-preview.1 for Unity 2019.x. See top sticky post about these new releases.