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

Auto-Fitting text

Discussion in 'UGUI & TextMesh Pro' started by Dam-Pete, Dec 8, 2014.

  1. Dam-Pete

    Dam-Pete

    Joined:
    Jun 11, 2010
    Posts:
    54
    As a long time user of NGUI, I used the feature of auto-fitting text some times to allow my text to freely rescale and have any border of it readjust to the new size (think of growing text balloons). As is in UUI (or whatever we're calling it), I haven't figured out any way to do this other than attempting to calculate an optimal size for the text components.

    Any suggestions? If needed I'll provide a sample project to further explain what I mean.
     
  2. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    There is a check-box on the text component called best fit, that should work for you.
    file:///Applications/Unity/Unity.app/Contents/Documentation/html/en/Manual/script-Text.html
     
  3. Dam-Pete

    Dam-Pete

    Joined:
    Jun 11, 2010
    Posts:
    54
    I have seen the Best Fit, but it doesn't resize the UI element, it rescales the font size to best fit the bounds of the element.

    I have also tested Horizontal/Vertical Overflow, which allows the text to overflow, but does not resize the Rect Transform properties, which makes it impossible to have a sliced border image that resizes to always surround the text. Unless I'm missing something...

    What I'm trying to achieve is a constant-sized font which always fits justly inside a background balloon. This balloon should resize according to the amount of text. The Overflow property allows the text UI element to be any size, but it doesn't update the Rect Transform, which makes anchoring the balloon to the text element have no practical effect.
     
  4. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    You can use the anchor system to fit the rectTransform to the size of the parent object.
     
  5. der_r

    der_r

    Joined:
    Mar 30, 2014
    Posts:
    259
    Or you could write a script that calculates the best width for the parent based on the length of the string.
     
  6. Dam-Pete

    Dam-Pete

    Joined:
    Jun 11, 2010
    Posts:
    54
    Here's an example of what happens. The first image is the text overflowing, but no element resizing. The second image is how I expected it to work (text element resizes, allowing the anchoring to work properly). Never mind the element rendering order (which is another extremely poor implementation of the new UI system, I cannot arbitrarily choose an anchor, I must use the hierarchy system, which also decides the rendering order, but let's leave that for another discussion).

    That's exactly what I wanted to avoid. The width depends not only on the length of the string, but the font size, for instance, which makes this calculation quite troublesome (not to mention other factors). This would make this system extremely easy to break.
     

    Attached Files:

    • ui-1.png
      ui-1.png
      File size:
      7.3 KB
      Views:
      1,949
    • ui-2.png
      ui-2.png
      File size:
      7.4 KB
      Views:
      2,724
  7. StefanoCecere

    StefanoCecere

    Joined:
    Jun 10, 2011
    Posts:
    210
    der_r likes this.
  8. Dam-Pete

    Dam-Pete

    Joined:
    Jun 11, 2010
    Posts:
    54
    Thanks, krur. I was just navigating checking if I could help out anyone else and I found this thread (http://forum.unity3d.com/threads/input-filed-fit-to-content.281359/), which mentioned this component. I tested it and indeed it works for my case. Now to solve the render order... :)
     
  9. der_r

    der_r

    Joined:
    Mar 30, 2014
    Posts:
    259
    Content Fitter component! Neato!