Author Topic: [SOLVED]UI Label line break \n \\n  (Read 15608 times)

Baroni

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
[SOLVED]UI Label line break \n \\n
« on: July 17, 2013, 05:39:53 PM »
Hi,

straight to the point: The typical line break on UI Labels doesn't work. I attached a screenshot of one of your examples named "Example X", running Unity version 3.5.5f2 or 4.1.5f1 and NGUI 2.6.3.

You can see the line break approach with \n in the image, however I searched on the forums and found that one should use \\n now, but that doesn't work either. Encoding checked or unchecked, same result. Please advice me how to proceed further, thanks.
« Last Edit: July 18, 2013, 04:23:04 AM by Baroni »

dlewis

  • Guest
Re: UI Label line break \n \\n, possible bug
« Reply #1 on: July 17, 2013, 11:48:42 PM »
If you are entering text into the inspector then just hit the enter key. When you add text through code then it will turn \n into a new line.

Baroni

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: UI Label line break \n \\n, possible bug
« Reply #2 on: July 18, 2013, 03:36:52 AM »
Thanks for the reply, you're right. Just tested a direct assignment and it works as expected.

The real issue, however, lies in the (indirect) assignment of an inspector string to the UILabel via code. I can't do something like this:

  1. public UILabel myUILabel;
  2. public string myText;
  3.  
  4. void Start ()
  5. {
  6.    myUILabel.text = myText;
  7. }

With a \n or \\n , the UILabel will draw the text just as it is. I can't press enter in the inspector for a string value.
Just to clarify: The \n version for strings gets displayed correctly in the free version of NGUI.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UI Label line break \n \\n, possible bug
« Reply #3 on: July 18, 2013, 04:02:13 AM »
So change your script:
  1. public UILabel myUILabel;
  2. public string myText;
  3.  
  4. void Start ()
  5. {
  6.    myUILabel.text = myText.Replace("\\n", "\n");
  7. }
The idea is that we don't want there to be a string.replace operation every time a string is assigned. It's slow and expensive and in most cases -- completely not necessary.

Baroni

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: UI Label line break \n \\n, possible bug
« Reply #4 on: July 18, 2013, 04:21:27 AM »
Great, thanks for the clarification.