Intelwalk
Intelwalk

Reputation: 671

UIPickerView Text Color Change In This Implementation

I had an app about a year and half ago I was fooling with that I decided to come back to it. When I left it the pickerview was visible and worked. Now it still works but the text color is black along with the background. How can I get the text color different with these functions?

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

Upvotes: 1

Views: 3653

Answers (3)

Dipen Chudasama
Dipen Chudasama

Reputation: 3093

Add this method may be help you.

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

}

Upvotes: 0

Rafał Sroka
Rafał Sroka

Reputation: 40038

To change the color of the title use:

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

This lets you provide an attributed string for the title.

Example

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

Upvotes: 0

Mkni1408
Mkni1408

Reputation: 125

You can just change the color for each label in the picker. See this thread for the answer: can I change the font color of the datePicker in iOS7?

Upvotes: 1

Related Questions