UIPickerView selected label shortcut color

I created a custom UIPickerView, but I would like UILabel to change color when scrolling to the selected row as follows:

UIPickerView

Any ideas?

Edit: What I would like to do is change the color of the UILabel during the selection, i.e. While the wheel is turning, not later.

Here is what I have so far that changed the color of UILabel after you made your choice:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { AILabel * pickerRow = view ? (AILabel *)view:[[[AILabel alloc] initWithFrame:CGRectMake(0, 0, 140, 40)] autorelease]; pickerRow.backgroundColor = [UIColor clearColor]; pickerRow.font = [UIFont boldSystemFontOfSize:18.0f]; pickerRow.insets = UIEdgeInsetsMake(0, 10, 0, 0); if(component == 0) { pickerRow.text = [self.numberArray objectAtIndex:row]; if ( row == number ) { pickerRow.alpha = 0.0f; [UIView animateWithDuration: 0.33f delay: 0.0f options: UIViewAnimationOptionCurveEaseOut animations:^{ pickerRow.textColor = [UIColor whiteColor]; pickerRow.shadowColor = [UIColor blackColor]; pickerRow.shadowOffset = CGSizeMake(0.0f, 1.0f); pickerRow.alpha = 1.0f; } completion:^(BOOL finished){ }]; } else { pickerRow.textColor = [UIColor blackColor]; pickerRow.shadowColor = [UIColor whiteColor]; pickerRow.shadowOffset = CGSizeMake(0.0f, 1.0f); } } else { pickerRow.text = [self.durationArray objectAtIndex:row]; if ( row == duration ) { pickerRow.alpha = 0.0f; [UIView animateWithDuration: 0.33f delay: 0.0f options: UIViewAnimationOptionCurveEaseOut animations:^{ pickerRow.textColor = [UIColor whiteColor]; pickerRow.shadowColor = [UIColor blackColor]; pickerRow.shadowOffset = CGSizeMake(0.0f, 1.0f); pickerRow.alpha = 1.0f; } completion:^(BOOL finished){ }]; } else { pickerRow.textColor = [UIColor blackColor]; pickerRow.shadowColor = [UIColor whiteColor]; pickerRow.shadowOffset = CGSizeMake(0.0f, 1.0f); } } return pickerRow; } 

AILabel is a regular UILabel, nothing special about it. The variable 'number' is the currently selected value in the first component. "Duration" is the current selected value in the second component.

Hope this is clearer

Greetings

+4
source share
1 answer
  - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { UILabel *label = (UILabel*) view; if (label == nil) { label = [[UILabel alloc] init]; } label.textColor = [UIColor RedColor]; CGSize rowSize = [pickerView rowSizeForComponent:component]; CGRect labelRect = CGRectMake (0, 0, rowSize.width, rowSize.height); [label setFrame:labelRect]; return label; } - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { UILabel *selectedRow = (UILabel *)[pickerView viewForRow:row forComponent:component]; selectedRow.textColor = [UIColor blueColor]; } 
-1
source

Source: https://habr.com/ru/post/1480064/


All Articles