0

I have a web service that brings some text data back. The idea is to extract the text and create UILabels for the text to show on screen. I however, do not know two things:

  1. How many labels are needed
  2. The length of the text

Due to having no prior knowledge of these things I need a way to create some labels that have the right length for the text it contains.

I've managed to store the data into some objects that are in an array and then iterate through them creating the labels.. something like this:

    for (BBItemAttributes *attribute in self.item.productAttributes){

        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 50, 10)];
        label.text = attribute.displayTemplate;
        [self.scrollView addSubview:label];
    }

Obviously the problem here is due to creating the UILabels in code and having each one a hard coded CGRect size they are all onto of each other and sometimes don't fit in their respective boxes due to text being too long.

I need a way to line the labels up all on the same X axis point and on different Y axis points, so that they sit next to each other a certain space apart.

Is there a better way to do this?

halfer
  • 19,824
  • 17
  • 99
  • 186
Robert J. Clegg
  • 7,231
  • 9
  • 47
  • 99
  • then, do not use static value for `y` in the `frame`, if you'd like to use different `y` in the `frame`. – holex May 21 '14 at 10:54
  • Thing is, each UILabel needs a different y value, which must be a certain number of points away from the last UILabel. That's what I am having difficulty with – Robert J. Clegg May 21 '14 at 10:58
  • what is wrong with such solution (e.g) `CGFloat y= 100;' then in the iteration increase it like `y = y + 10.f;` and the frame is `CGRectMake(100, y, 50, 10)`... I'm not seeing any difficulties here so far. – holex May 21 '14 at 11:15

3 Answers3

1

Try this. it will help you.

float yAxis = 0;
for (int i = 0; i< 50; i++)//for (BBItemAttributes *attribute in self.item.productAttributes){
{
    CGSize size;
    UIFont *font = [UIFont systemFontOfSize:15.0]; // Set Your Font
    //Your String = attribute.displayTemplate

    if (ios7)//Condition to check if ios7
    {
        //iOS 7
        CGRect frame = [@"Your String" boundingRectWithSize:CGSizeMake(50, CGFLOAT_MAX)
                                          options:NSStringDrawingUsesLineFragmentOrigin
                                       attributes:@{NSFontAttributeName:font}
                                          context:nil];
        size = CGSizeMake(frame.size.width, frame.size.height+1);
    }
    else
    {
        //iOS 6.0
        size = [@"Your String" sizeWithFont:font constrainedToSize:CGSizeMake(50, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
    }
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, yAxis, 50, size.height)];

    label.text = @"Your String";//attribute.displayTemplate
    label.numberOfLines = 0;
    label.tag = i; // Set tag if you want to access in future. :)
    [self.scrollView addSubview:label];

    // Increase yAxis
    yAxis = yAxis + size.height + 10;//10 is extra space if you want between two label
}

// Do Not forget to set Contentsize of your ScrollView.
self.scrollView.contentSize = CGSizeMake(320, yAxis + 20);
ChintaN -Maddy- Ramani
  • 5,156
  • 1
  • 27
  • 48
1

If your showing a data structure that is a like a list of labels, I would suggest that you use UITableViewController for showing the list.It has the built in facility and properties that make it easy to display a list of objects.

Now the answer to your first Question.You can get the number of labels by determining the number of objects that are in your array. Something like this :

NSInteger *noOfLabels = yourArray.count;

As far as the length of the text is concerned, I suggest not keeping it very lengthy because Labels are not good for lengthy texts. You can however, specify the number of a lines for a particular label in case if it has lengthy text.This will make the text appear in two lines. Though you will have to adjust width and height accordingly.

You can get the text length by doing something similar to this :

NSString *text;
NSInteger *i = (NSInteger*)[text length];

Hope this helps.

Dhrumil
  • 3,221
  • 6
  • 21
  • 34
  • Thanks. I need to display the labels as bits of non-interactive info for the user. They provide more context to the content and have no other actions besides that. So a UITableView would not be suitable. As for the number of labels - thats a good idea with the array count. Thanks! – Robert J. Clegg May 21 '14 at 11:25
1

You need to do something like this;

CGFloat yOrigin = 100;
CGFloat fixedSpace = 10;
for (BBItemAttributes *attribute in self.item.productAttributes)
{
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, yOrigin, 50, 10)];
    label.text = attribute.displayTemplate;
    label.numberOfLines = 0;
    [label sizeToFit]; // Resizes label to fit the text.
    [self.scrollView addSubview:label];
    yOrigin += label.frame.size.height + fixedSpace; 
 }
ujell
  • 2,792
  • 3
  • 17
  • 23
  • This is what I need. However I see I got mixed up with Y and X axis. I actually wanted the Y axis to stay the same and the X axis to change. I modified your code and have it working perfectly now. Thank you! Nice and simple! – Robert J. Clegg May 21 '14 at 11:23
  • Is there a way to give the labels extra width? Since I am setting the background colour, I would like the labels to be a little longer than the text it contains? – Robert J. Clegg May 21 '14 at 11:38
  • Look at this question: http://stackoverflow.com/questions/3476646/uilabel-text-margin – ujell May 21 '14 at 11:39
  • Thanks. I managed to do it without subclassing: Just modified your code some more – Robert J. Clegg May 21 '14 at 11:45
  • Well, i'm glad this helped :) – ujell May 21 '14 at 11:55