Search Unity

Question TextMeshPro UI text auto realigning after adding a character of a bigger font in char by char typing

Discussion in 'UGUI & TextMesh Pro' started by Cactys, Mar 23, 2024.

  1. Cactys

    Cactys

    Joined:
    Jan 3, 2022
    Posts:
    1
    I have a slight problem with my printing char by char to a TextMeshPro Text (UI) field. Everything works great except when I try to use the rich text editor feature <font=x> to make my text a bigger size (showcased by the red text in the side by size of the same text field before and after text of a bigger font is added). When I add the first character of a bigger size my text auto-resizes itself to align the top of the biggest font to the top of the TMP text box, which causes the smaller text to shift down slightly. If possible I would like to avoid this.

    Currently I have my text set to "Top" alignment which is how I would like my text to align (to the top of the text box). If I set my text to "Baseline" alignment that naturally fixes the issue because the smaller and bigger font have the same baseline alignment, but then my text is in the middle of the text box and the text shifts up when a new line is added, which is not what I want.

    I can think of a possible solution where I add an invisible character (or a space) of the max font size that will be displayed in the set of characters to be printed so it's already offset before I print the big characters, but If there are any fixes that would make baseline alignment or another easier fix like that work I would appreciate hearing them.

    Thanks!

    Side by side of the text being printed out in game view, I added the red and gray line so you can see the height difference
    upload_2024-3-23_19-13-30.png
     

    Attached Files:

  2. xucian

    xucian

    Joined:
    Mar 7, 2016
    Posts:
    846
    isn't it just a thing about the vertical alignment + the vertical size of the TMPro text itself?
    I imagine setting "align top" + increasing the height of your text would make the bigger letters be lower than the small letters (so their 'top' will be aligned). I imagine that's what you want. If not, the only possibility is wanting to align them in the center vertically (doesn't that work by just setting 'align center' in inspector?)