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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

make UI element adaptive to text content

Discussion in 'UGUI & TextMesh Pro' started by MindSpyke, Jan 22, 2016.

  1. MindSpyke

    MindSpyke

    Joined:
    Sep 13, 2013
    Posts:
    14
    So : I put a panel on my canvas in which in put 2 text objects , I want the text objects to scale in width
    according to the text
    (panel also has to scale)

    for example : when there's a short string of text I want a small panel window that's just wide enough for the text , if it's a long string .... a wider panel , wide enough to fit the text

    Any effiecient way of doing this ?

    many thanks !
     
  2. SterlingSoftworks

    SterlingSoftworks

    Joined:
    May 16, 2015
    Posts:
    8
    Have you tried attaching a "Content Size Fitter" component to your text? Attaching one of these and changing the settings to "Preferred" will change the rect trans to fit the content. If you set the Horizontal to "Unconstrained" the text will get to the edge of the original rect trans and go to the next line (if you don't want one long string, which I don't think you do ;P)

    Then you can try writing up a script that changes the panel's size based on the text's rect trans :D
     
    Kiwasi likes this.
  3. MindSpyke

    MindSpyke

    Joined:
    Sep 13, 2013
    Posts:
    14
    ok thanks man :)
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    For regular text objects the content size fitter works fine. For input fields you'll need to do some more trickery. I currently manage it by comparing InputFeild.text.Length with InputFeild.TextComponent.text.Length.