Change UIPickerView pickerView:viewForRow: attributes based on a certain condition

StackOverflow https://stackoverflow.com/questions/23289851

  •  09-07-2023
  •  | 
  •  

Domanda

I am trying to change the text color in viewForRow based on a certain condition. When I press a button the view changes to a different color but I would also like to change the colour of the text in the picker. I use 'viewForRow' because I have a custom view.

//adds subcategories to the wheel
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 44)];
    label.backgroundColor = [UIColor clearColor];


    if (!self.isBackgroundWhite)//Boolean that changes when the background changes
    {
        NSLog(@"Set white text");
        label.textColor = [UIColor whiteColor];

    }else{

        label.textColor = [UIColor blackColor];
    }



    if (row == 1) {
        label.text = [NSString stringWithFormat:@"  %@",[self.servicesArray objectAtIndex:row]];
        label.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18];
    }else{
        label.text = [NSString stringWithFormat:@"     -%@",[self.servicesArray objectAtIndex:row] ];
        label.font = [UIFont fontWithName:kAppFont size:16];
    }
        return label;
}

EDIT: Thanks to @Rich I was able to spot my problem, I just need to call [self.pickerView reloadAllComponents];

È stato utile?

Soluzione

If you only want to change the text color just use the other delegate method pickerView:attributedTitleForRow:forComponent:

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
{

    NSString *title;
    UIFont *font;
    if (row == 1) {
        title = [NSString stringWithFormat:@"  %@",[self.servicesArray objectAtIndex:row]];
        font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18];
    } else{
        title = [NSString stringWithFormat:@"     -%@",[self.servicesArray objectAtIndex:row] ];
        font = [UIFont fontWithName:kAppFont size:16];
    }

    UIColor *textColor;
    if (self.isBackgroundWhite) {
        textColor = [UIColor blackColor];
    } else {
        NSLog(@"Set white text");
        textColor = [UIColor whiteColor];
    }

    NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    paragraphStyle.alignment = NSTextAlignmentLeft;

    NSDictionary *attributes = @{NSForegroundColorAttributeName : textColor, 
                                            NSFontAttributeName : font,
                                  NSParagraphStyleAttributeName : paragraphStyle};

    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}

Also a note that you should call [self.pickerView reloadAllComponents]; when changing isBackgroundWhite. I would do this by overriding the setter for backgroundWhite and when the boolean value changes reload the picker view.

EDIT:
There appears to be a 'bug' (intentional or not) in iOS 7 (works fine on iOS 6) with setting the UIFont on an NSAttributedString returned from the pickerView:attributedTitleForRow:forComponent: method. So while the above works for the text color the font does not change. Luckily you can get around this with the code in the question. See this answer for more info.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top