How to change text color when selected in uipickerview?

守給你的承諾、 提交于 2019-12-11 19:18:51

问题


I want to change the color of the row that the selection of the uipickerview lands on. There is another question like this but the only answer is showing how to make all the text in the uipickerview changed to a certain color. I just want the text when selected in the viewer to appear a different color.

-(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;

}

回答1:


How about using this delegate method instead:

- (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];
}


来源:https://stackoverflow.com/questions/22649740/how-to-change-text-color-when-selected-in-uipickerview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!