简体   繁体   中英

How to change multiple UILabel text colour while scrolling inside scrollview Objective-c iOS

Hi I am new to iOS development..Can any one help me.. I have added multiple UILabel inside UIScrollview based on array count..For example if array count is 3 means ..then In scrollview adding 3 view along with UILabel in each view..

So now 3 view having 3 different UILabels..

But now I want to change colour of UILabel text in different views based on requirement..but I am not able to update colour…

Its change colour of UILabel text only for last index ..

I have written code in ScrollViewDidScroll:(UIScrollView *)scrollView

Any suggestion ..

self.robotScrollView.contentSize = CGSizeMake(robotCounts*Robot_ScrollView_Width, kRobotSrollViewH);
for (int i=0; i<robotCounts; i++) {
    self.robotLabel = [[UILabel alloc] initWithFrame:CGRectMake(Robot_ScrollView_Width*i, 0 , Robot_ScrollView_Width, kRobotSrollViewH)];
    self.robotLabel.textAlignment = NSTextAlignmentCenter;
    self.robotLabel.backgroundColor = [UIColor clearColor];
    self.robotLabel.textColor = [UIColor blackColor];
    [self.robotScrollView addSubview:self.robotLabel];
    if (kRemoteManager.robotsArray.count == 0) {
        self.robotLabel.text = @"";
        break;
    }
    DeviceBase *robot = kRemoteManager.robotsArray[i];
    self.robotLabel.text = [NSString stringWithFormat:@"%@",robot.dName];

}
self.robotScrollView.contentOffset = CGPointMake(Robot_ScrollView_Width*currentRobotIndex, 0);

Changing UIlabeltext color in below scrollviewdidscroll method

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == self.scrollView)
    {
        self.pageControl.currentPage = (scrollView.contentOffset.x+(scrollView.width/2)) / scrollView.width;
        kRemoteManager.currentIndex = self.pageControl.currentPage;
        [[NSNotificationCenter defaultCenter] postNotificationName:kNotify_Steward_ReloadData object:nil];
        NSMutableArray *viewRemoteIDarray = [NSMutableArray array];
        CardRemoteModel *model =  kRemoteManager.cardRemoteArray[self.pageControl.currentPage];
            if (![viewRemoteIDarray containsObject:@(model.remoteID)])
            {
                if (model.remoteType == kooKong_remoteType_AirCleaner || model.remoteType == kooKong_remoteType_AC)
                {
                    self.robotLabel.textColor = [UIColor whiteColor];
                }
            else
                {
                self.robotLabel.textColor = [UIColor blackColor];
                }
            }
    } else if (scrollView == self.robotScrollView) {
        kRemoteManager.currentRobotIndex = (scrollView.contentOffset.x+(scrollView.width/2)) / scrollView.width;
        self.leftBtn.hidden = NO;
        self.rightBtn.hidden = NO;
        if (kRemoteManager.currentRobotIndex == kRemoteManager.robotsArray.count-1) {
            self.rightBtn.hidden = YES;
        }
        if (kRemoteManager.currentRobotIndex == 0){
            self.leftBtn.hidden = YES;
        }
    }
}

There are three labels, but only one label property. The last one assigned is the only one you'll have access to later. One solution would be to keep an array of labels in the view controller.

@property(nonatomic, strong) NSMutableArray *labels;

In the posted method...

self.labels = [NSMutableArray array];
for (int i=0; i<robotCounts; i++) {
    UILabel *robotLabel = [[UILabel alloc] initWithFrame:CGRectMake(Robot_ScrollView_Width*i, 0 , Robot_ScrollView_Width, kRobotSrollViewH)];

    [self.labels addObject:robotLabel];                 // <--- new

    robotLabel.textAlignment = NSTextAlignmentCenter;
    robotLabel.backgroundColor = [UIColor clearColor];
    robotLabel.textColor = [UIColor blackColor];
    [self.robotScrollView addSubview:robotLabel];
    if (kRemoteManager.robotsArray.count == 0) {
        robotLabel.text = @"当前无酷控机器人";
        break;
    }
    DeviceBase *robot = kRemoteManager.robotsArray[i];
    robotLabel.text = [NSString stringWithFormat:@"%@",robot.dName];
}

To change all of the colors:

- (void)setLabelColors:(UIColor *)color {
    for (UILabel *label in self.labels) {
        label.textColor = color;
    }
}

Another idea would be to give each label a tag, and find them when you need them.

for (int i=0; i<robotCounts; i++) {
    self.robotLabel = [[UILabel alloc] initWithFrame:CGRectMake(Robot_ScrollView_Width*i, 0 , Robot_ScrollView_Width, kRobotSrollViewH)];
    self.robotLabel.tag = i+1;
    // the remainder of this loop as you have it

To change all the colors...

- (void)setLabelColors:(UIColor *)color {
    for (int i=0; i<robotCounts; i++) {
        UILabel *label = (UILabel *)[self.robotScrollView viewWithTag:i+1];
        label.textColor = color;
    }
}

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