Author Topic: Get UILabel Y Size?  (Read 1844 times)

Chaosgod_Espér

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Get UILabel Y Size?
« on: January 14, 2016, 12:36:46 PM »
Hi there..

I want to change the position of UILabels in my Scrollview, to make them sit directly under each other.
The UILabels can be rewritten by UIInput, and so they can change in height (cause multiline).

So the Question:
How to get the current Height of a Label, to place the next directly under it.

Current approach:
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class UISetup_ReloadTextView : MonoBehaviour {
  5.        
  6.         private int ChildCounts, NewYPosition;
  7.         private Transform CurrentChild;
  8.        
  9.         public void UpdateView(){
  10.                 ChildCounts = transform.childCount;
  11.                 NewYPosition = 0;
  12.                
  13.                 GetComponent<UIScrollView>().ResetPosition();
  14.                 for(int looper1 = 0; looper1 < ChildCounts; looper1 ++){
  15.                         CurrentChild = transform.GetChild(looper1);
  16.                        
  17.                         CurrentChild.position = new Vector3(CurrentChild.position.x, NewYPosition, 0);
  18.                         NewYPosition -= (int)(CurrentChild.GetComponent<UILabel>().relativeSize.y * CurrentChild.localScale.y);
  19.                 }
  20.                 GetComponent<UIScrollView>().ResetPosition();
  21.         }
  22. }
Problem:
if Label1 has a Widget Y size of 48, Label2 is placed far below it (around 200)... instead of 48 :/
« Last Edit: January 14, 2016, 02:17:59 PM by Chaosgod_Espér »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Get UILabel Y Size?
« Reply #1 on: January 16, 2016, 01:22:30 PM »
I would just use a UITable with 1 column, and put all my labels underneath it. When text changes, Reposition() the table.