【问题标题】:Change Color of UIPickerView Text更改 UIPickerView 文本的颜色
【发布时间】:2014-07-22 02:39:24
【问题描述】:

我有一个 UIPickerView,我想根据这个堆栈溢出问题中显示的方法将文本的颜色更改为白色:How do I change the color of the text in a UIPickerView under iOS 7?

但是,我输入的代码似乎不起作用。谁能告诉我为什么?

我的 ViewController.m 中有以下方法

- (NSAttributedString *)changePickerColor:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component :(NSString*)title
 {
     NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];

     return attString;
}

在我的 viewDidLoad 中,我写了这段代码:

self.instruments = [[NSMutableArray alloc]initWithObjects:@"Baritone",@"Trumpet",@"French Horn",@"Tuba", nil];
for (int i = 0; i < self.instruments.count; i++) {
    [self changePickerColor:self.pickerView attributedTitleForRow:i forComponent:0 :self.instruments[i]];
}

我没有收到任何错误消息,代码根本不起作用。 请帮忙,谢谢。

【问题讨论】:

  • pickerView:attributedTitleForRow:forComponent: 是协议定义的委托方法——它的命名必须完全一样(就像你链接到的答案一样)。此外,您不应该自己调用它——选择器视图会调用它(只要它的命名正确)。
  • 试试这个,stackoverflow.com/questions/18807940/…这可能对你有帮助

标签: ios objective-c ios7 uipickerview


【解决方案1】:
  • 或者你也可以使用下面的委托并定义一个 UILabel 来设置你喜欢的文本颜色

    – pickerView:viewForRow:forComponent:reusingView:
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多