Search Unity

Resizing text box to fit text

Discussion in 'Scripting' started by Camronas, Aug 20, 2017.

  1. Camronas

    Camronas

    Joined:
    Apr 20, 2012
    Posts:
    154
    Hello everyone,

    I am trying to make an inventory tooltip where the items stats and abilities fill out a tooltip panel. My system thus far works reasonably well but it is a bit clunky and I want to learn a better way to do it.

    Code (CSharp):
    1.         int textLength = textToAdd.Length;
    2.  
    3.         currentToolTipSize = new Vector2(currentToolTipSize.x + ((_toolTipText.fontSize * textLength) / _toolTipText.fontSize), currentToolTipSize.y + ((_toolTipText.fontSize * textLength) / _toolTipText.fontSize));
    The goal is that every new ability/variable such as durability fits on it's own line. This works but it often makes the tooltip box to long which is an unfortunate side effect.

    Any help would be really appreciated!
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    There is the best fit option for text size, and the content size fitter for box size.
     
    TractGames and thendralp like this.
  3. Camronas

    Camronas

    Joined:
    Apr 20, 2012
    Posts:
    154
    Best fit isn't really what I am needing sadly. I need the Tooltip box to change in size as items might have more or less information to display.

    Using best fit would give me on occasion text that is just tiny or the text would be massive to fill the space. That or huge amounts of dead space.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Which is why he also mentioned the content size fitter for box size....

    The box can adjust horizontally or vertically based on the text you put in it.
     
  5. joshcantcode

    joshcantcode

    Joined:
    Aug 20, 2017
    Posts:
    6
    View your Canvas, and use the four anchors on your UI elements. It'll take some adjusting, but it will resize to any screen. I'd recommend editing these on a 16:9 ratio to ensure it resizes correctly.
     
  6. FranSabba

    FranSabba

    Joined:
    Sep 9, 2019
    Posts:
    1
    Content size filter doesn't work with text boxes, they just ignore it
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Works just fine on a text box. I have used it several times.