Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to word-wrap Label text?

Discussion in 'UI Toolkit' started by ShadyMike, Sep 17, 2019.

  1. ShadyMike

    ShadyMike

    Joined:
    Apr 14, 2013
    Posts:
    60
    Hi,

    I cannot seem to figure out how to wrap a label's text if it exceeds its parent's content area. What is the recommended way to do so?

    Thanks in advance.
     
  2. pirho_luke

    pirho_luke

    Joined:
    Oct 24, 2017
    Posts:
    21
    Use the uss property

    Label
    {
    white-space: normal;
    }


    Unintuitive I know but that's what it does!
     
  3. SaiNarayan_SG

    SaiNarayan_SG

    Unity Technologies

    Joined:
    May 13, 2021
    Posts:
    2
    Have come back to this innumerable times whenever I forget the name of the property. Thanks!
     
    gutschow likes this.
  4. pokelocos

    pokelocos

    Joined:
    Nov 9, 2015
    Posts:
    54
    What is the way to do this same thing from a C# script?
     
  5. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    443
    There are multiple ways of doing it programmatically. You could do something like this when initializing a field, but this would apply the style inline.
    Code (CSharp):
    1. Label label = new Label()
    2. {
    3.     style =
    4.     {
    5.         fontSize = 12,
    6.         whiteSpace = WhiteSpace.Normal
    7.     }
    8. };
    Alternatively, you could still rely on USS like so:

    USS:
    Code (CSharp):
    1. .wrap
    2. {
    3.    white-space: normal;
    4. }

    C#:
    Code (CSharp):
    1. label.AddToClassList(".wrap");
     
    pokelocos likes this.