开发者

Change UIPicker's row color or row text color

开发者 https://www.devze.com 2023-03-22 00:30 出处:网络
I have a UIPicker I\'m using to toggle layers on and off. I want to be able to either change the color of a row within the picker to indicate if that layer is on, or to change the font color of the te

I have a UIPicker I'm using to toggle layers on and off. I want to be able to either change the color of a row within the picker to indicate if that layer is on, or to change the font color of the text to indicate if that layer is on.

If neither are possible, I will append the nsstring to add an X at the end of it before I return it (so "streets" w开发者_如何学运维ould become "streets X" if the layer is on). I'm not sure how to do this either. I know you can do this type of buffing with a number, but not sure with a NSString. I know the picker can display 27 characters, so maybe set the string length to 26 and then set the char at location 26 to X? But I don't know how to set the length of a string.

Any ideas for either path?


For custom rows in a UIPickerView you have to implement pickerView:viewForRow:forComponent:reusingView: instead of pickerView:titleForRow:forComponent:.

In your case you'd just use a UILabel as the view, and change the text colour and text string for each row.

Assuming you only have one component:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *label;
    if (!view) // No view to re-use, create a new one
        label = [[UILabel alloc] initWithFrame://your frame here];
    else
        label = (UILabel*)view;

    label.text = // Your row text here
    if (//some flag to indicate that this row is selected)
        label.textColor = [UIColor redColor];
    else
        label.textColor = [UIColor blackColor];

    return label;

}
0

精彩评论

暂无评论...
验证码 换一张
取 消