Search Unity

Displaying Padded Strings on UI Text

Discussion in 'UGUI & TextMesh Pro' started by chino01, Jun 17, 2015.

  1. chino01

    chino01

    Joined:
    Jul 19, 2014
    Posts:
    13
    Hello everyone,

    I don't have a problem with using PadLeft, PadRight or string.Format. I have strings ,but my problem is displaying them aligned on UI without doing complicated layout groups and other things. This really bugged me so much and couldn't find the solution anywhere.

    Isn't there an easy way to achieve this padded text on UI? Without dividing the strings to several text objects or using several layout groups?
    Thanks so much for your time.
     

    Attached Files:

    • sa.png
      sa.png
      File size:
      29.1 KB
      Views:
      910
  2. Stephan-B

    Stephan-B

    Joined:
    Feb 23, 2011
    Posts:
    2,269
    I realize you are looking for a solution using UI.Text but I thought it was an interesting problem that I would like to tackle with TextMesh Pro which is a replacement for UI.Text.

    Here is an image showing the text object and the text input string to achieve this using the latest release of TextMesh Pro for Unity 4.6 / Unity 5.0.

    upload_2015-6-16_16-55-0.png

    To create the two columns of text, I am using the <pos> tag which can be defined as pixel (px) or font (em) units or as a percentage of the text container / RectTransform width.

    Although this works, I would like to find a more elegant implementation as I don't like having to use the monospacing tag and having to add an extra space in front of some numbers.

    Thinking out loud ... You still need to use the <pos> tag which ensures the columns remain aligned as the RectTransform changes size. Basically a new tag or way to define both the alignment and padding of the numbers / characters contained between this tag would be required.
     
  3. chino01

    chino01

    Joined:
    Jul 19, 2014
    Posts:
    13
    Hello, I thought I replied from mobile but sorry I just figured I haven't. Thanks for your effort on trying and finding a solution with TextMesh Pro, I know it's one of the best asset on the store. I haven't got it yet. On my wish list, though :)

    I tried with a monospace font but it's just not worth the effort so I impemented an UI solution seperating the columns.

    Thanks again for your reply.
     
  4. hurleybird

    hurleybird

    Joined:
    Mar 4, 2013
    Posts:
    258
    Did you ever come up with anything? Pos tag is still very fiddly and not a great solution except when you know specifically the number of digits, size of the rect, font size, etc. IMO, it's as bad as layout fiddling, just in different ways.

    What about a tag that takes two arguments: position and alignment. So for example: pos=100 + align=right would create column at far right side of the rect. Pos=50 + align=center would create a column right in the center of the rect (while Pos=50 + align=left would create a column that starts right at the center of the rect just prior to the first character).

    This would be a really massive help in a lot of situations.