0

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];

Rich
  • 8,108
  • 5
  • 46
  • 59
DevC
  • 6,982
  • 9
  • 45
  • 80

1 Answers1

2

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.

Community
  • 1
  • 1
Rich
  • 8,108
  • 5
  • 46
  • 59
  • I need all the titles to be on aligned left not centre? Is there a way to do that here? – DevC Apr 25 '14 at 10:25
  • 1
    @Gman yep, you can use `NSParagraphStyleAttributeName`, I'll add that. – Rich Apr 25 '14 at 10:26
  • @Gman check out [`NSMutableParagraphStyle`](https://developer.apple.com/library/ios/documentation/Cocoa/Reference/ApplicationKit/Classes/NSMutableParagraphStyle_Class/Reference/Reference.html#//apple_ref/occ/cl/NSMutableParagraphStyle) for more options to set on the `paragraphStyle` object :) – Rich Apr 25 '14 at 10:29
  • Thanks Rich, its almost perfect accept the font does not change, it is @"HelveticaNeue-Bold" and size 18 for all – DevC Apr 25 '14 at 10:33
  • I got my original code working with [self.pickerView reloadAllComponents] :) – DevC Apr 25 '14 at 10:36
  • @Gman ah I did wonder what your exact problem was - your question was a bit vague :p glad you got it all working though! I'll check out the font issue :) – Rich Apr 25 '14 at 10:42
  • 1
    @Gman nevermind it appears to be a 'bug' (sort of) with iOS 7 when using the `UIFont`, I've updated the answer with a note to others! – Rich Apr 25 '14 at 10:50