在PickerView中滚动时更改TextColor

时间:2016-03-28 13:17:58

标签: ios iphone xcode uiscrollview datepicker

我想更改选择器视图的TextColor。我可以在调用didSelectRow方法之后更改textColor。

但是当文本进入selectedRow区域时,我也希望更改TextColor,就像iPhone中的Clock app一样。

mycode的:

-(NSAttributedString*)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSAttributedString *str= [[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"%02ld",row] attributes:@{
                                                                                                                             NSForegroundColorAttributeName:[UIColor redColor]
                                                                                                                             }];
    if([pic selectedRowInComponent:component]==row)
    str= [[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"%02ld",row] attributes:@{
                                                                                                                             NSForegroundColorAttributeName:[UIColor greenColor]
                                                                                                                             }];
    return str;
}


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{

    [pic reloadComponent:component];
}

我如何实现这一目标?

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以在示例中使用多个PickerView实现所需的效果,即中心的主PickerView具有大型和差异化颜色。然后在上方和下方放置两个较小的拾取器视图,确保所有这些都受到限制,以便只能看到一行高度。确保只设置主对象的委托并在View层次结构中将其转发,当手动滚动时,会偏移其他选择器的内容。

相关问题