我有一个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]];
}


我没有收到任何错误消息,该代码根本无法正常工作。
请帮助,谢谢。

最佳答案

或者,您也可以使用以下委托并定义UILabel来设置文本的首选颜色

– pickerView:viewForRow:forComponent:reusingView:

关于ios - 更改UIPickerView文本的颜色,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24878020/

10-12 00:16
查看更多