UILabel - Displaying data in Multiple Labels

By : Imran
Source: Stackoverflow.com

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

By : Imran

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.


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

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.

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];

This video can help you solving your question :)
By: admin