UIPickerView:NSAttributedString在iOS 7中不可用?

看来UIPickerView不再支持使用NSAttributedStringselect器视图项目。 任何人都可以确认吗? 我在UIPickerView.h文件中发现NS_AVAILABLE_IOS(6_0) ,但这是问题吗? 有没有办法解决这个问题,还是我运气不好?

 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component; - (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component NS_AVAILABLE_IOS(6_0); // attributed title is favored if both methods are implemented - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view; 

这个问题的唯一解决scheme显然是使用pickerView:viewForRow:forComponent:reusingView:并返回带有属性文本的UILabel,因为Apple显然已经禁用了使用属性的string。

Rob是正确的,错误或不是在iOS 7 UIPickerView中获取归属文本的最简单方法是破解pickerView:viewForRow:forComponent:reusingView:方法。 这是我做的…

 -(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { // create attributed string NSString *yourString = @"a string"; //can also use array[row] to get string NSDictionary *attributeDict = @{NSForegroundColorAttributeName : [UIColor whiteColor]}; NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:yourString attributes:attributeDict]; // add the string to a label's attributedText property UILabel *labelView = [[UILabel alloc] init]; labelView.attributedText = attributedString; // return the label return labelView; } 

它在iOS 7上看起来不错,但是在iOS 6中默认的背景是白色的,所以你看不到我的白色文本。 我build议检查iOS版本,并根据每个实现不同的属性。

下面是一个使用pickerView:viewForRow:forComponent:reusingView的例子:以一种尊重循环视图的方式。

 - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UILabel *)recycledLabel { UILabel *label = recycledLabel; if (!label) { // Make a new label if necessary. label = [[UILabel alloc] init]; label.backgroundColor = [UIColor clearColor]; label.textAlignment = NSTextAlignmentCenter; } label.text = [self myPickerTitleForRow:row forComponent:component]; return label; }