Author Topic: UILabel Multiline help!  (Read 4907 times)

Yuemari

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 12
    • View Profile
UILabel Multiline help!
« on: August 20, 2013, 04:31:45 PM »
Hi there, I've been working with some UILabels, but right now I can't make the multiline effect from code.

I'm using ngui version 2.6.1e on Unity 3.5

Here is my C# code:

  1. public UILabel myLabel;
  2.  
  3. public void SwitchText()
  4. {
  5.     myLabel.text = "Some text Line_1 \n  Some text Line2";
  6. }
  7.  


But when I hit the play button and execute the function all I see in the game screen is literally that text, without multiline, the UILabel settings are attached on an image, but anyway here they are:

Max Width = 0;
Max Lines = 0; //I've tried with 2 and obtained the same result.
Shrink To Fit = false;
Encoding = true;//I've tried with false and obtained the same result.
Password = false;
Effect: Outline;

Any help would be nice!

Thanks!


Yuemari

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 12
    • View Profile
Re: UILabel Multiline help!
« Reply #1 on: August 20, 2013, 04:51:41 PM »
Actually I´m doing some tests and my code is this:

  1. public UILabel myLabel;
  2. public string myText; //the text on the editor is : Line 1 \n Line 2
  3. public SwitchText()
  4. {
  5.     myLabel.text = myText;
  6. }
  7.  
  8.  

When I assigne the text directly like:

  1. public UILabel myLabel;
  2.  
  3. public SwitchText()
  4. {
  5.     myLabel.text = "Line 1 \n Line 2";
  6. }
  7.  

It works just fine, any issue I'm not seeing?

Thanks in advance!

Yuemari

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 12
    • View Profile
Re: UILabel Multiline help!
« Reply #2 on: August 20, 2013, 05:02:45 PM »
Seems that the only way to make it work right is doing this as in other topic (http://www.tasharen.com/forum/index.php?topic=5450.0)

  1. public UILabel myLabel;
  2. public string myText; // Line 1 \n Line 2
  3.  
  4. public void SwitchText()
  5. {
  6.      myLabel.text = myText.Replace("\\n","\n");
  7. }
  8.  
  9.  

with this it just work fine but it'd be nice if this wasn't necessary as Replace operation its expensive. if you know another way to make it work, let me know please.

Thanks

OnlineCop

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 51
    • View Profile
Re: UILabel Multiline help!
« Reply #3 on: August 20, 2013, 05:38:21 PM »
Are you putting a literal "\n" in the Editor? You shouldn't actually see "\n" in the Editor: you should see a literal newline in the Inspector box.

Take a snapshot of what you see in the Inspector if this isn't the case.

Yuemari

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 12
    • View Profile
Re: UILabel Multiline help!
« Reply #4 on: August 21, 2013, 09:26:20 AM »
Hi there, I'm actually watching the "\n" this is because the text will be edited by someone else and right now I haven't develop some Editor CustomGUI for the script that the designer will be editing.

I attach the image of string variable I'm currently using to set the UILabel .text

Thanks!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UILabel Multiline help!
« Reply #5 on: August 21, 2013, 09:51:11 AM »
You don't need a custom editor for that. Just add a [Multiline] tag in front, like so:
  1. [Multiline] public string text = "Hello World";

Yuemari

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 12
    • View Profile
Re: UILabel Multiline help!
« Reply #6 on: August 21, 2013, 10:01:59 AM »
Ohh, I wasn't aware of that, I'll add it.

Thanks!

Yuemari

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 12
    • View Profile
Re: UILabel Multiline help!
« Reply #7 on: August 21, 2013, 10:17:01 AM »
Hmm looks like the [Multiline] only works for Unity 4, and I'm currently using 3.5  :( .

Thanks anyway! Im aware now that Unity 4 support Property Drawers.

OnlineCop

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 51
    • View Profile
Re: UILabel Multiline help!
« Reply #8 on: August 21, 2013, 10:27:04 AM »
So someone else will be editing the text. Will they be doing that from within the Unity Inspector itself?

Or, can they save their text to a file somewhere and have you read it in via code, and assign it with 'myLabel.text = ...' ?

If they are going to be directly modifying the text from the Unity Inspector, then yes, you'll have to .Replace() like you mentioned. Because what your code sees is the escaped value of that: "... in \n order to..." would be "... in \\n order to" (double '\\' instead of a single '\').

All my developers save all their text into a text file, and I parse that file myself. Then all of their literal '\n' in text turn into natural newlines in the Inspector.

Yuemari

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 12
    • View Profile
Re: UILabel Multiline help!
« Reply #9 on: August 21, 2013, 10:36:20 AM »
That sounds nice!, I'll check the priorities and maybe we can do that too.

I understand the problem now, thanks!!