Author Topic: Perform Anchor calculations immediately before Start()?  (Read 4289 times)

Tiktaalik

  • Newbie
  • *
  • Thank You
  • -Given: 7
  • -Receive: 0
  • Posts: 39
    • View Profile
Perform Anchor calculations immediately before Start()?
« on: October 21, 2014, 04:40:55 PM »
I have a situation where I want to have UILabel (WidgetA) with a child anchored to it (WidgetB) that will wrap around the UILabel. When I set a bunch of text to the UILabel, the label will expand for the text and I want to immediately have the child WidgetB update its anchors and expand to contain WidgetA. I want this to occur immediately so that I can immediately query the new size of WidgetB. How do I force these calculations Immediately?

As it stands to get this to work I can call

  1. WidgetA.text = value;
  2. //We must use "localSize" here as UILabels recalculate their text processing on when localSize is called.
  3. //Calling localSize will force this recalculation immediately.
  4. var size = WidgetA.localSize;
  5. WidgetB.UpdateAnchors();
  6. var sizeB = WidgetB.localSize; //<-- not going to be correct.
  7. ....
  8.  

However at this point sizeB will not be correct. Later on in the Start() I'll be able to fetch the size of WidgetB and it'll be correct.

So my core issue is that I'm not really clear on how Anchors work at the low level. Is there's some aspect of their functionality that requires on the objects being active or enabled and UpdateAnchors() doesn't get around this? Is there a way to force the calculations immediately even if I'm calling on objects prior to their Start()?


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Perform Anchor calculations immediately before Start()?
« Reply #1 on: October 22, 2014, 05:30:39 AM »
Have you seen the EnvelopContent script? It was written to do just this: automatically envelop arbitrary content (example: tooltip background).

UIWidget.localSize won't update the label right away. You need to either access UILabel's printedSize, or call ProcessText().