更改 UIPicker 的行颜色或行文本颜色

发布于 2024-11-25 11:00:31 字数 306 浏览 1 评论 0原文

我有一个 UIPicker,用于打开和关闭图层。我希望能够更改选择器中行的颜色以指示该图层是否打开,或者更改文本的字体颜色以指示该图层是否打开。

如果两者都不可行,我将附加 nsstring 以在返回之前在其末尾添加一个 X(因此,如果图层打开,“街道”将变为“街道 X”)。我也不知道该怎么做。我知道您可以使用数字进行这种类型的抛光,但不确定是否使用 NSString。我知道选择器可以显示 27 个字符,所以也许将字符串长度设置为 26,然后将位置 26 处的字符设置为 X?但我不知道如何设置字符串的长度。

对于这两条路有什么想法吗?

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" would 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

古镇旧梦 2024-12-02 11:00:31

对于 UIPickerView 中的自定义行,您必须实现 pickerView:viewForRow:forComponent:reusingView: 而不是 pickerView:titleForRow:forComponent:

在您的情况下,您只需使用 UILabel 作为视图,并更改每行的文本颜色和文本字符串。

假设您只有一个组件:

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

}

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;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文