UIPickerView 选定行标签颜色

Posted

技术标签:

【中文标题】UIPickerView 选定行标签颜色【英文标题】:UIPickerView Selected Row Label Color 【发布时间】:2013-05-10 07:58:37 【问题描述】:

我已经制作了一个自定义 UIPickerView,但我希望 UILabel 在像这样滚动到所选行时改变颜色;

有什么想法吗?

编辑: 我想做的是更改 UILabel 的颜色在进行选择时,即在***转动时,而不是之后。

这是我目前所得到的,它会在你做出选择后改变 UILabel 的颜色:

- (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 只是一个自定义的 UILabel,没什么特别的。 'number' 变量是第一个组件中的当前选定值。 'duration' 变量是第二个组件中当前选择的值。

希望这更清楚

干杯

【问题讨论】:

***.com/questions/7595220/… 没有任何代码,帮助你很复杂...... 粘贴您的代码以便我们为您提供帮助 我已经更新了问题并包含了代码。不要认为我说得不够清楚我想要什么 【参考方案1】:
 - (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];

【讨论】:

以上是关于UIPickerView 选定行标签颜色的主要内容,如果未能解决你的问题,请参考以下文章

如何更改 UIPickerView 中选定行的颜色

在 Swift 中为选定的 UIPickerView 行设置动画背景颜色

在 UIPickerView 中更改选定行的颜色,滚动时也是如此

从 UIPickerView 的选定行设置 UIButton 的标题

如何根据标签显示 UIPickerView 的选定值

显示带有选定行的 UIPickerView