0

Hi I have data coming from JSON and i am displaying it in UILabel. But if there is no data for particular value, the UILabel remains empty with white space.

How can i remove UILabel white space and make below UILabel to take place of first label and so on.. for remaining UILabel.

enter image description here

ChenSmile
  • 3,401
  • 4
  • 39
  • 69

7 Answers7

3

1.Set the Leading and Trailing constraints for all the label.

2.For First label set the top constraint.

3.Set the vertical spacing for other label with the value 0.

Note:

Don't set the height for the Labels.

First output:

enter image description here

Second output:(i set empty text for 2nd and 4th labels)

enter image description here

0

If you don't want to use a tableview, you could have two properties for each label, i.e. the top and height constraint and set the constants to 0 for a label if there is no value. Not the most elegant solution, but should work. I can provide an example if necessary, good luck.

JingJingTao
  • 1,760
  • 17
  • 28
  • Actually what I'm suggesting is not a good idea, too many constraints to keep track of if you have lots of labels and it won't work well if the first label's top constraint is different from the top constraint of the labels below, I think what vishnuvarthan or Muruganandam.S will be better. – JingJingTao Sep 07 '16 at 11:01
0

If you dont want to use a table view then you can have all your labels in a view and iterate the views subviews and set text to them.By this your empty lables will be the last ones. You can tweak it a little to suite your requirement.

-(void)addLableTextWith :(NSArray*)jsonList
 {
   NSArray * lblArray = [YourContainerView subviews];

   for (int i = 0; i <[jsonList count] ; i++)
    {
    UILabel * youurLbl = (UILabel*)lblArray[i];
    youurLbl.text = jsonList[i];
    }
 }
vishnuvarthan
  • 492
  • 1
  • 6
  • 23
0

Try to set constraints and use height constraint. Set this constraint value <= label height. So this label is set according to text between max and 0.

0

A simpler solution would be to use a single label, in this case names lblDetails and build up the displayed text like this

    var sDetails = ""

    // parse sLine1, sLine2, etc., from JSON

    // build up the string you need

    if sLine1 != ""
    {
        sDetails = sLine1
    }
    if sLine2 != ""
    {
        sDetails = sDetails + "\n" + sLine2
    }
    if sLine3 != ""
    {
        sDetails = sDetails + "\n" + sLine3
    }

    lblDetails.text = sDetails
Russell
  • 5,436
  • 2
  • 19
  • 27
-1
CGRect secondFrame = secondLabel.frame;
secondLabel.hidden = YES;
thirdLabel.frame = secondFrame;

If you have 4 labels and you want to hide 2nd label and at the place of 2nd label you want third label than do something like this.

KAR
  • 3,303
  • 3
  • 27
  • 50
-1

You can change position of any UILabel at run time. Keep in mind your screen is at fourth quadrant. Example code:

CGRect frame = labelName.frame;
frame.origin.y=10;//y value, horizondal
frame.origin.x= 12;//x value vertical
labelName.frame= frame;`

Using this facility you can do the same.

Saranjith
  • 11,242
  • 5
  • 69
  • 122
  • i have 15 uilabels then, i will make a whole bunch of code in each condtion – ChenSmile Sep 07 '16 at 10:29
  • In this case there is only 5 uilabels, If youre having many labels i would prefer doing it with layouts instead. [tutorial by apple](https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwjdtNGCh_3OAhUILY8KHaIRDOsQFggdMAA&url=https%3A%2F%2Fdeveloper.apple.com%2Flibrary%2Fios%2Fdocumentation%2FUserExperience%2FConceptual%2FAutolayoutPG%2F&usg=AFQjCNG3ykOGI4t0LAm1HSaUX9pE17IZSA&sig2=GkMpDDr2Ihv-g0C9-60JYQ) – Saranjith Sep 07 '16 at 10:34