Search Unity

TextMesh Pro TextMeshProUGUI.GetPreferredValues - Problem?

Discussion in 'Unity UI (uGUI) & TextMesh Pro' started by Aleksander87, Sep 19, 2018.

  1. Aleksander87

    Aleksander87

    Joined:
    Jul 7, 2017
    Posts:
    16
    I'm using TextMeshProUGUI.GetPreferredValues(myText, myMaxWidth, 0) to get the text height.
    Sometimes the method calculates a wrong height. The error gives me an height of a text that has a line less than what it is in the reality.
    I'm thinking I'm using it in the wrong way.
    The text has many tags: <uppercase>, <font="another font">, <size=70%> and <align=right>.
    It is one of these tags that lead to my error maybe?

    Thank you!
     
  2. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    3,314
    If you can provide me with the exact text that is producing the incorrect results, I can verify it.

    Make sure you test this with font assets included with TextMesh Pro as custom / other font assets would have different sizes / metrics and could produce different results.
     
  3. Aleksander87

    Aleksander87

    Joined:
    Jul 7, 2017
    Posts:
    16
    Yes, the font it's the text mesh pro built in one.
    Here an example:
    Schermata 2018-09-20 alle 09.34.44.png Schermata 2018-09-20 alle 09.33.55.png
    As you can see from the images most of the baloons, has a correct height but the baloon with numbers has a wrong height.
    Here it is the string:

    <uppercase><color=#64002E><font="Curse Casual SDF">GUEST 4783446</font></uppercase></color>
    <size=100%>123456789 123456788 123456789 123445679 123456778 </size>
    <align=right><size=70%> 16:40

    In this example the width that I give to the GetPreferredValues method is: 800

    Thank you for your help!
     
  4. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    3,314
    Finally got a chance to look into this.

    Since I don't have this "Curse Casual SDF" font asset, I used <font="Anton SDF"> instead.

    I used the following line in the text input box to then compare with the results coming from preferred values.

    "<uppercase><color=#64002E><font="Anton SDF">GUEST 4783446</font></uppercase></color>\n<size=100%>123456789 123456788 123456789 123445679 123456778</size>\n<align=right><size=70%>16:40"

    upload_2018-9-25_0-50-38.png

    This text object has a width of 800 and height of 206.

    Code (csharp):
    1. Vector2 values = m_TextComponent.GetPreferredValues("<uppercase><color=#64002E><font=\"Anton SDF\">GUEST 4783446</font></uppercase></color>\n<size=100%> 123456789 123456788 123456789 123445679 123456778</size>\n<align=right><size=70%>16:40", 800, 0);
    The above function returned the correct height of 206.00.

    Are you able to reproduce this with Liberation Sans and Anton SDF?

    What height do you get for the RectTransform height in the Editor vs. using the GetPreferredValues() function.
     
  5. Aleksander87

    Aleksander87

    Joined:
    Jul 7, 2017
    Posts:
    16
    So, I tried again and maybe I did a mistake with the width (800), because it was correct this time.
    I tried many different widths until I got the error again. This time with a width of 500.
    I tried with both fonts but the result is the same.
    The editor gives me an height of 361.47, and the Debug.Log value of the function GetPreferredValues() is 297.97 (with Anton)

    Thanks!
     

    Attached Files:

  6. su9257

    su9257

    Joined:
    Jun 13, 2017
    Posts:
    16
    https://forum.unity.com/threads/tex...d-seems-to-not-take-font-into-account.498348/

    Hope can help you