简体   繁体   中英

Insert NSArray of strings into UILabel and count by 1.

How Would I insert an array of strings into UILabel , and have them load or count by 1? I'm trying to load the following array into the UILabel :

self.array = [NSArray arrayWithObjects:@"Testing 1",@"Testing 2",@"Testing 3", nil];

Here I am using an UIScrollView that automatically changes from each slide to each slide:

 for (int i=1; i<=3; i++) {

        // create label
        UILabel *text = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,30)];
        text.text = [NSString stringWithFormat:@"%@", self.array];
        text.font=[UIFont fontWithName:@"HelveticaNeueLTStd-LtCn" size:15];
        text.textColor = [UIColor darkGrayColor];
        text.textAlignment =NSTextAlignmentCenter;
        // create imageView
        UIImageView *lblView = [[UIImageView alloc] initWithFrame:CGRectMake((i-1)*scrMain.frame.size.width, 325, 280, 240)];

        // set label
        [lblView addSubview:text];

        // apply tag to access in future
        lblView.tag=i+1;

        // add to scrollView
        [scrMain addSubview:lblView];
    }

I have an example of how to do it with UIImageView :

for (int i=1; i<=3; i++) {
        // create image
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"sti%02i.png",i]];
        // create imageView
        UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake((i-1)*scrMain.frame.size.width + 23, 60, 280, 260)];
        // set scale to fill
        imgV.contentMode=UIViewContentModeScaleToFill;
        // set image
        [imgV setImage:image];
        // apply tag to access in future
        imgV.tag=i+1;
        // add to scrollView
        [scrMain addSubview:imgV];
    }

The images are in my project and are numbered sti01, sti02, and so on... It then counts by one for each images and then loads them into the scrollview. I hope this helps!

The y-position of the label should be adjusted according to the index.

You can use the array count instead of a fixed '3'.

You don't need an UIImageView in between.

You can try this:

for (int i = 0; i < self.array.count; i++) {

    // create label
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, i*30, self.view.frame.size.width,30)];
    label.text = [self.array objectAtIndex:i];
    label.font = [UIFont fontWithName:@"HelveticaNeueLTStd-LtCn" size:15];
    label.textColor = [UIColor darkGrayColor];
    label.textAlignment = NSTextAlignmentCenter;

    // add to scrollView
    [scrMain addSubview:label];
}

If I'm understanding you correctly, you need to make the following change:

text.text = [NSString stringWithFormat:@"%@", [self.array objectAtIndex:i]];

This will pull out the i th object from your array of strings and set that as the text of your UILabel .

Why don't you loop through the array instead of trying to assign the text to the whole array like this:

for (int i=0; i<3; i++) {

    // create label
    UILabel *text = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,30)];
    text.text = [NSString stringWithFormat:@"%@", [self.array objectAtIndex:i]];
    text.font=[UIFont fontWithName:@"HelveticaNeueLTStd-LtCn" size:15];
    text.textColor = [UIColor darkGrayColor];
    text.textAlignment =NSTextAlignmentCenter;
    // create imageView
    UIImageView *lblView = [[UIImageView alloc] initWithFrame:CGRectMake((i-1)*scrMain.frame.size.width, 325, 280, 240)];

    // set label
    [lblView addSubview:text];

    // apply tag to access in future
    lblView.tag=i+1;

    // add to scrollView
    [scrMain addSubview:lblView];
}

That way you're just pulling that one string out, assigning it as the UILabel's text, then adding it as a subview to the appropriate imageView.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM