Can't get UIPicker text color

I am trying to get UIPicker

that which shows the selected row with green text. I am using a method pickerView:viewForRow:forComponent:reusingView:

to create a UILabel for it:

- (UIView*) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *label = (UILabel*)view;
    if (label == nil) {
        label = [UILabel new];
        label.font = [UIFont boldSystemFontOfSize: 24];
        label.adjustsFontSizeToFitWidth = YES;
    }
    UIColor *color = (row == [pickerView selectedRowInComponent: component]) ? RGBx(0x579B2F) : UIColor.whiteColor;
    label.textAlignment = component == 0 ? NSTextAlignmentRight : NSTextAlignmentLeft;
    NSMutableAttributedString *string;
    if (component == 0) {
        string = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%3lu.", row]];
        [string addAttribute:NSForegroundColorAttributeName value: color range:NSMakeRange(0, string.length - 1)];
        [string addAttribute:NSForegroundColorAttributeName value: [UIColor clearColor] range:NSMakeRange(string.length - 1,1)];
    }
    else {
        string = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@".%02lu", row % 60]];
        [string addAttribute:NSForegroundColorAttributeName value: [UIColor clearColor] range:NSMakeRange(0,1)];
        [string addAttribute:NSForegroundColorAttributeName value: color range:NSMakeRange(1,string.length - 1)];
    }
    label.attributedText = string;
    return label;
}

      

There are some additional things to align the two wheels closely, but with a little spacing (transparent periods). It basically works:

enter image description here

Initially it looks perfect, with green highlights and white / gray others. The problem is that when I scroll the wheels, they don't always turn out to be green. This happens sometimes, but not always. As you can see, sometimes the scrollable value will remain green even if it is no longer selected (see 09

top right corner).

What should I do to keep only green and always on the selected row?

+3


source to share


2 answers


@idali diagnoses the problem well. And hints at what should happen. But didn't give an actual answer.

The answer lies in using reloadComponent:

(or reloadAllComponents

). This should be done anytime the user changes a selection, or programmatically changes a value. For example.

-(void) pickerView:(UIPickerView*) pickerView didSelectRow:(NSInteger) row inComponent:(NSInteger) component {
    [pickerView reloadComponent: component];
}

      



and anytime you use selectRow:inComponent:animated:

:

...
[self.picker selectRow: hours inComponent: 0 animated: YES];
[self.picker reloadAllComponents];
...

      

0


source


If there was a green line first, if you scroll up or down while that line is still visible, it will keep green, since the viewForRow: (NSInteger) line for the component: will not be called on that line (this will be if this line was not previously was visible and should be visible) ...



while you need to set the selected RowinCompoment green to the selected one, you also need to remove the green from the old selected row (in case it is still visible).

+1


source







All Articles