簡體   English   中英

在選擇器視圖中更改行顏色

[英]Change row color in picker view

有什么辦法可以改變選擇器視圖對象在行中的顏色?

例如,您有addobject:@"black"但是如何將字體顏色更改為紅色?

使用UIView方法代替創建“標題”委托方法,而是創建並填充自己的視圖。 因此,您創建了一個320寬(比如說44高)的UIView,並在其中放置了標簽,設置了標簽文本和顏色,現在您已經擁有了想要的東西。

編輯:

#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