此实现中的 UIPickerView 文本颜色更改

UIPickerView Text Color Change In This Implementation

大约一年半前我有一个应用程序,当时我正在玩弄它,所以我决定重新使用它。当我离开它时,pickerview 是可见的并且可以工作。现在它仍然有效,但文本颜色与背景一起是黑色的。如何使用这些函数使文本颜色不同?

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

    if (component == SIZE)
        return [arraySize count];
    if (component == MEASURE)
        return [arrayMeasure count];
    return 0;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component == SIZE)
        return [arraySize objectAtIndex:row];
    if (component == MEASURE)
        return [arrayMeasure objectAtIndex:row];
    return 0;
}

您可以在选择器中更改每个标签的颜色。请参阅此主题以获得答案:can I change the font color of the datePicker in iOS7?

要更改标题的颜色,请使用:

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView
             attributedTitleForRow:(NSInteger)row
                      forComponent:(NSInteger)component

这样您就可以为标题提供属性字符串。

例子

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView  
             attributedTitleForRow:(NSInteger)row 
                      forComponent:(NSInteger)component
{
    return [[NSAttributedString alloc] initWithString:@"Your title"              
                                           attributes:@
    {
        NSForegroundColorAttributeName:[UIColor redColor]
    }];
}

添加此方法可能对您有所帮助。

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSAttributedString *attString  = nil;
    NSString *title = @"";

    if (component == SIZE)
        title = [arraySize objectAtIndex:row];
    if (component == MEASURE)
        title = [arrayMeasure objectAtIndex:row];
   attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName:[UIColor greenColor]}];

   return attString;

}