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

时间:2015-01-21 21:35:26

标签: ios iphone uipickerview

我有一个应用程序大约一年半以前我在愚弄我决定回到它。当我离开它时,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;
}

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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

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

}