【问题标题】:UIPickerView Selected Row Label ColorUIPickerView 选定行标签颜色
【发布时间】: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' 变量是第二个组件中当前选择的值。

希望这更清楚

干杯

【问题讨论】:

  • 没有任何代码,帮助你很复杂......
  • 粘贴您的代码以便我们为您提供帮助
  • 我已经更新了问题并包含了代码。不要认为我说得不够清楚我想要什么

标签: iphone ios objective-c xcode uipickerview


【解决方案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];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    相关资源
    最近更新 更多