Author Topic: UILabel ResizeHeight: height property has wrong value  (Read 15287 times)

gianis6

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 5
    • View Profile
UILabel ResizeHeight: height property has wrong value
« on: June 13, 2014, 05:57:23 AM »
Hello,

I am using UILabel with Overflow property set to 'ResizeHeight' as a Prefab. Every time I instantiate a new one label of this type I try to read the property height but its value is wrong (while the height shown in the editor is correct)

Here is the piece of code I use to get the Label's height:
  1. UILabel textIng = Instantiate(textsLabel) as UILabel; // textsLabel is the prefab I use
  2. textIng.text = "my dynamic text here sometimes it can be very long";
  3. textIng.transform.parent = topAnchor; // topAnchor is the anchor where I attach the label
  4. textIng.transform.localScale = new Vector3(1f, 1f, 1f);
  5. textIng.transform.localPosition = new Vector3(25f, currentY, 0); // currentY is an incremental variable I use to display one label below another
  6.  
  7. Debug.Log(textIng.height); // this returns the wrong height value while in the editor I can see it correct
  8.  

Any help would very appreciated
Thanks in advance

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UILabel ResizeHeight: height property has wrong value
« Reply #1 on: June 13, 2014, 06:11:27 AM »
The label won't be resized until you access UILabel's processedText property, which is accessed when you call UILabel's printedSize or localSize properties.

You should never use Instantiate() by the way. You need to use NGUITools.AddChild(parent, prefab), or just NGUITools.AddChild(parent). Look inside the function to find out why and what it does.

gianis6

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: UILabel ResizeHeight: height property has wrong value
« Reply #2 on: June 13, 2014, 07:32:03 AM »
I modified the script but it seems to have not solved the problem. The height value I get is still wrong.
Here is the snippet:
  1. GameObject goTextIn = NGUITools.AddChild(topAnchor.gameObject, textsLabel);
  2. UILabel textIn = goTextIn.GetComponent<UILabel>();
  3. textIn.text = "dynamic text which can be very long";
  4. textIn.transform.localScale = new Vector3(1f, 1f, 1f);
  5. textIn.transform.localPosition = new Vector3(25f, currentY, 0);
  6. Vector2 tmpSize = textIn.localSize; // also tried with printedSize and processedText properties
  7. Debug.Log(textIn.height); // the value is still wrong
  8.  

Am I missing something?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UILabel ResizeHeight: height property has wrong value
« Reply #3 on: June 13, 2014, 08:20:12 PM »
What's the "wrong" value and what's the "right" value?

If you're looking for the value shown in inspector, that's just label.height.

gianis6

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: UILabel ResizeHeight: height property has wrong value
« Reply #4 on: June 14, 2014, 10:38:45 AM »
Reading the label.height property the way I posted returns a bigger value than the one shown in the editor. It seems that if try to read the height property just after I set the text of the label I get an incorrect value like if the label hadn't resized yet.
I tried to read the height property in a second moment (not just after having set the text) and the value I got is correct, so I think the problem is related to the fact of reading "too early".
Are there any ways to solve this problem?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UILabel ResizeHeight: height property has wrong value
« Reply #5 on: June 15, 2014, 05:22:11 AM »
As I mentioned, you need to make the text get processed first before you try to access the height.

In your case I suggest making UILabel's "ProcessText()" public so that you can call it. I'll make it public in 3.6.4b for you.
  1. label.text = "My new text\nNext line";
  2. label.ProcessText();
  3. Debug.Log(label.height);

gianis6

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: UILabel ResizeHeight: height property has wrong value
« Reply #6 on: June 15, 2014, 04:01:51 PM »
I tried also the method you posted in the last post, but the value continues to be wrong.
I add som extra info in case it can help.  The label I'madding are placed inside a scrollview

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UILabel ResizeHeight: height property has wrong value
« Reply #7 on: June 16, 2014, 08:13:21 AM »
I just did a quick test.

1. New scene.
2. ALT+SHIFT+L, Overflow "Resize Freely"
3. Attached this script and hit Play:
  1. using UnityEngine;
  2.  
  3. public class Test : MonoBehaviour
  4. {
  5.         void Start ()
  6.         {
  7.                 UILabel label = GetComponent<UILabel>();
  8.                 label.text = "My new text\nNext line";
  9.                 label.ProcessText();
  10.                 Debug.Log(label.height);
  11.         }
  12. }
Debug.Log shows "40", as expected (label's font size is 20).

Andresfdezb

  • Newbie
  • *
  • Thank You
  • -Given: 10
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: UILabel ResizeHeight: height property has wrong value
« Reply #8 on: November 03, 2014, 09:45:45 AM »
I'm bumping this because I have the very same problem. Here's my code (it goes inside a foreach that cycles through some children labels):

  1. labelName = child.GetComponent<UILabel>();
  2. if (labelName != null) {
  3.                 labelName.text = item.GetName();  // returns a string with the name of the current item
  4.                 labelName.ProcessText();
  5.                 // labelName.ResetAndUpdateAnchors();
  6.                 Debug.Log(labelName.processedText + ": " + labelName.height);
  7. }

So the Log shows the label text and a height of 32 (font size is 16). In the log, the processed text is displayed in 2 lines, but in the game view (and also the editor) the text is displayed in 3 lines and shows a size of 48 (which is logical, since it's 3 lines of 16 size font).
I tried the simple example of a new empty scene and it works correctly, so any idea of what may have I done wrong? I set the bottom anchor of the label a few lines later, can it be the reason?

  1. // If the label takes more than one line, reposition it related to the type label ("name label" is above "type label")
  2. if (labelName.height > 16) {
  3. // the widget is the one that contains the two labels
  4.         labelName.bottomAnchor.Set(widget.transform, (16.0f + labelType.height) / widget.height, 0.0f);
  5.         labelName.ResetAndUpdateAnchors();
  6.         labelType.topAnchor.Set(widget.transform, (11.0f + labelType.height) / widget.height, 0.0f);
  7.         labelType.ResetAndUpdateAnchors();
  8. }
  9. // The values 16 and 11 contain the upper+middle+bottom padding of each label inside the widget

[edit] By the moment I change the anchors, the label.height is already wrong (it shows wrong the very next line of the labelName.ProcessText()) sot the anchoring is not the problem.

[edit 2] Debugging it shows that after calling ProcessText() the value of mProcessedtext includes two lines but the value of mText doesn't (it's all one line). But still, in game mode and editor mode it displays the label as a 3 line label. The values of height are correct, since editor panel shows a height of 48 for the 3 line label but Log shows a height of 32 for a 2 line label. Weird...

[edit 3] Attached 2 screenshots. One showing the label.height as 32 in the log (don't mind the error, I coded it as error but it should be a warning, lol) and the other showing the 3 line label and a size of 48. Both screens happening at the same time.
« Last Edit: November 03, 2014, 10:26:52 AM by Andresfdezb »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UILabel ResizeHeight: height property has wrong value
« Reply #9 on: November 04, 2014, 09:34:02 AM »
What is it you're trying to do, and what version of NGUI are you using?

Andresfdezb

  • Newbie
  • *
  • Thank You
  • -Given: 10
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: UILabel ResizeHeight: height property has wrong value
« Reply #10 on: November 05, 2014, 03:03:09 AM »
What is it you're trying to do, and what version of NGUI are you using?
I'm using v3.7.1.

What I'm doing is populating a table with rows instantiated at runtime (using Addchild function). Those rows have one sprite and 2 labels (name and type) and the content of the labels is read from a file. Since the width of the table is fixed, the labels must resize their height depending on the contents, therefore the widget that contains the row must change its size according to the size of the labels (plus a certain padding: 11px above upper label, 5px between upper and lower label and 11px below lower label). I can't set (or I think I can't set) the anchors of the widget relative to the labels because when the name label is just one line tall, then the icon on the left is higher than the sum of the two labels plus the padding (attached an image showing the two possible cases).

It works perfectly in all the cases but the one in the previous pictures, in which the value of label.height is equal to 2 lines tall but the label is displayed in 3 lines (and the size of the label equals 3 lines tall). As the size of the widget is set by code depending on the sum of the label.height of both labels (plus padding) and label.height returns a "wrong" value, the size of the widget is set wrong and the contents of the row overlaps the previous table row (picture of the previous post).

[edit] I'm trying to set the anchors of the widget to the icon or the labels depending on the size of the labels instead of setting the size of the widget by code. I'll post the results later.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UILabel ResizeHeight: height property has wrong value
« Reply #11 on: November 06, 2014, 01:36:11 AM »
Table's cells are based on the content within them. If your widgets resize based on the content of the labels, you should use EnvelopContent. That script is capable of resizing a widget around multiple ones. That said, mixing such resizable content and tables may give unpredictable results (which one will execute first?) unless you control it (potentially manually). Although reading the rest of your description I am not sure what a table has to do with any of this, or why you're using it.

I don't advise mixing full anchors and resizable content. You are just asking for trouble. Instead, anchor only one corner (for example top-left). The right and bottom will be calculated based on the label's resized values. Just make sure that the label's pivot point matches (top left).

Andresfdezb

  • Newbie
  • *
  • Thank You
  • -Given: 10
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: UILabel ResizeHeight: height property has wrong value
« Reply #12 on: November 06, 2014, 02:31:25 AM »
Table's cells are based on the content within them. If your widgets resize based on the content of the labels, you should use EnvelopContent. That script is capable of resizing a widget around multiple ones. That said, mixing such resizable content and tables may give unpredictable results (which one will execute first?) unless you control it (potentially manually). Although reading the rest of your description I am not sure what a table has to do with any of this, or why you're using it.

I don't advise mixing full anchors and resizable content. You are just asking for trouble. Instead, anchor only one corner (for example top-left). The right and bottom will be calculated based on the label's resized values. Just make sure that the label's pivot point matches (top left).

Thanks for the response, I'll try with the EnvelopContent or setting the top-left corner. The reason I'm using a table is because some of the rows of the table get enabled/disabled depending on the element the user has clicked on, so table rows get positioned by the table itself.
Again, thanks. I'll keep working on it.