Search Unity

UGUI: How to get the text-width in pixels?

Discussion in 'UGUI & TextMesh Pro' started by col000r, Jul 21, 2016.

  1. col000r

    col000r

    Joined:
    Mar 27, 2008
    Posts:
    699
    How can I get the width of a Text? Not the width of the RectTransform, but the width of the actual Text-string inside the UI.Text. (So I can cut it short and add ... if it doesn't fit)

    I can do it in NGUI: label.printedSize.x
    But how can I do it in UGUI?
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
  3. col000r

    col000r

    Joined:
    Mar 27, 2008
    Posts:
    699
    Thanks a lot for the pointer!
    The CalculateLengthOfMessage didn't work for me - characterInfo.advance only returns 0 here, but text.preferredWidth actually did the trick with Horizontal Overflow set to Overflow!
     
  4. shady_lyf321

    shady_lyf321

    Joined:
    Jan 25, 2018
    Posts:
    3
    could you please share your solution? I met the same question,And I tried characterInfo, it worded but sometimes would fail.