【问题标题】:How to change text color when selected in uipickerview?在uipickerview中选择时如何更改文本颜色?
【发布时间】:2014-03-26 01:23:34
【问题描述】:

我想更改 uipickerview 的选择所在行的颜色。还有另一个这样的问题,但唯一的答案是显示如何将 uipickerview 中的所有文本更改为某种颜色。我只是希望在查看器中选择文本时显示不同的颜色。

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent: (NSInteger) component reusingView:(UIView *)view
{
    NSString *rowItem =[thing objectAtIndex:row];
    UILabel *lblRow = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView bounds].size.width, 44.0f];
    [lblRow setTextColor: [UIColor grayColor]];
    [lblRow setTextAlignment:NSTextAlignmentCenter];
    [lblRow setText:rowItem];

    [lblRow setBackgroundColor:[UIColor clearColor];

    return lblRow;

}

【问题讨论】:

    标签: iphone uipickerview


    【解决方案1】:

    改用这个委托方法怎么样:

    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
        NSString *rowItem =[thing objectAtIndex:row];
        UILabel *lblRow = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView bounds].size.width, 44.0f];
        [lblRow setTextColor: [UIColor grayColor]];
        [lblRow setTextAlignment:NSTextAlignmentCenter];
        [lblRow setText:rowItem];
    
        [lblRow setBackgroundColor:[UIColor clearColor];
    }
    

    【讨论】:

    • 你会调用这个方法吗,因为我刚刚用 4 个组件实现了它,它不起作用。
    • 它是 UIPickerViewDelegate 协议的委托方法。如果您的视图控制器设置为实现此协议,那么您可以提供此方法的实现,当用户选择组件中的一行时,它将被调用。
    • 我输入了一个 NSLog 并显示出来,但由于某种原因它没有改变颜色...
    • 他错过了将标签添加到选择器
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多