简体   繁体   English

在选择器视图中更改行颜色

[英]Change row color in picker view

Is there any way to change picker view object color in row? 有什么办法可以改变选择器视图对象在行中的颜色?

For example you have addobject:@"black" but how to change font color in red? 例如,您有addobject:@"black"但是如何将字体颜色更改为红色?

Instead of using the "title" delegate method, you use the UIView method, and you create and populate your own view. 使用UIView方法代替创建“标题”委托方法,而是创建并填充自己的视图。 So you create a UIView of 320 wide by say 44 high, put a label in it, set the label text and color, and now you have exactly what you want. 因此,您创建了一个320宽(比如说44高)的UIView,并在其中放置了标签,设置了标签文本和颜色,现在您已经拥有了想要的东西。

EDIT: 编辑:

#define PICKER_VIEW_SIZE CGSize(300, 40)

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return PICKER_VIEW_SIZE.height;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    return PICKER_VIEW_SIZE.width;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    // assume one component, but this solutions works for multiple
    // lets do red, green, and blue alternating backgrounds with alternating black and white text

    UIColor *backgroundColor;
    switch(row % 3) {
    case 0:
    default:
        backgroundColor = [UICOlor redColor];
        break;
    case 1:
        backgroundColor = [UIColor blueColor];
        break;
    case 2:
        backgroundColor = [UIColor greenColor];
        break;
    }
    UIColor *textColor = (row % 2) ? [UIColor blackColor] : [UIColor whiteColor];

    UIView *view = [[UIView alloc] initWithFrame:(CGRect){ {0,0}, PICKER_VIEW_SIZE}];
    view.backgroundColor = backgroundColor;

    UILabel *label = [UILabel alloc] initWithFrame:CGRectMake(10, 10, 200, 20)];
    // configure the label
    label.text = text appropriate for this row;

    [view addSubview:label];

    return view;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM