Author Topic: How to adjust size of a background to fit its chidren?  (Read 8183 times)

cheuksh

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
How to adjust size of a background to fit its chidren?
« on: January 04, 2014, 05:12:31 PM »
I have a background (UISprite), which have two children, one is call title (UILabel) another call paragraph (UILabel).
Because the paragraph have multiline, the height is varied. How can I adjust the height of the background to fit this? I tried UIStretch, but it only allow resizing the background according to one child object but not all children.
Because this background is an scroll item inside a scroll view. The height of the background will be used to make all of them position correctly in a scrollview. So I cannot simply separate the background for title and paragraph......

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to adjust size of a background to fit its chidren?
« Reply #1 on: January 04, 2014, 06:36:43 PM »
UIStretch is a legacy script. You should be using the new anchoring system. If the height of the title is fixed, then make the background anchored to the label on all sides so that it stretches when you resize the label. Learn the new layout system. It's powerful.

cheuksh

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: How to adjust size of a background to fit its chidren?
« Reply #2 on: January 04, 2014, 09:12:46 PM »
Sorry my title should be " How to adjust size of a parent to fit its chidren?"

The new UIRect Anchor is powerful, but it can only adjust child positions and size according to its parent container, cannot vice versa. as Now, I want to adjust the parent size to tightly fit all its children and the parent is actually a scroll-able row item ..(think of a facebook wall posts every post have a different height, depending on the post length)..If I anchor the parent top to the child top, the parent and all children will move upward forever...infinite loop happen. I guess all children position some how is actually depend on its parents..

Basically, It just something like: parent scrollable row Height = child title height + child paragraph height. (title anchor to the row top, paragraph anchor to the title bottom)...So cannot anchor scollable row bottom back to child paragraph bottom...it will cause infinite loop...

In addition.I tried to use the new anchor system, but once I upgrade my ngui, all my child sprite alpha value inherited from parents sprite. Which is not the case in the previous version.
Let's say I have a half transparent background sprite with alpha 0.5, which contains a few labels, like title and paragraph. Then All those label will become half transparent as well......That's why I don't want to upgrade to the latest version.....
« Last Edit: January 04, 2014, 09:22:21 PM by cheuksh »

cheuksh

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: How to adjust size of a background to fit its chidren?
« Reply #3 on: January 04, 2014, 09:55:00 PM »
Anyway, it is not difficult to calculate that by code...just wanna ask if there is some already exist. it is fine if there isn't .Ngui is great!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to adjust size of a background to fit its chidren?
« Reply #4 on: January 05, 2014, 06:58:40 AM »
The layout system isn't limited to having the parent as the target. You can set the target to be whatever you want, even children if you're so inclined (although it would be an odd thing to do and I recommend using siblings instead).

Game Object
- UILabel (title)
- UILabel (content, set to Resize Height overflow)
- UISprite (background, anchored to the content label, padded more up top than other sides to account for the title)