2014-04-25 6 views
0

J'essaie de changer la couleur du texte en viewForRow en fonction d'une certaine condition. Lorsque j'appuie sur un bouton, la vue change de couleur mais je voudrais aussi changer la couleur du texte dans le sélecteur. J'utilise 'viewForRow' parce que j'ai une vue personnalisée.Modifier UIPickerView pickerView: viewForRow: attributs basés sur une certaine condition

//adds subcategories to the wheel 
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view 
{ 
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 44)]; 
    label.backgroundColor = [UIColor clearColor]; 


    if (!self.isBackgroundWhite)//Boolean that changes when the background changes 
    { 
     NSLog(@"Set white text"); 
     label.textColor = [UIColor whiteColor]; 

    }else{ 

     label.textColor = [UIColor blackColor]; 
    } 



    if (row == 1) { 
     label.text = [NSString stringWithFormat:@" %@",[self.servicesArray objectAtIndex:row]]; 
     label.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18]; 
    }else{ 
     label.text = [NSString stringWithFormat:@"  -%@",[self.servicesArray objectAtIndex:row] ]; 
     label.font = [UIFont fontWithName:kAppFont size:16]; 
    } 
     return label; 
} 

EDIT: Merci à @ Rich j'ai pu repérer mon problème, j'ai juste besoin d'appeler [self.pickerView reloadAllComponents];

+0

Voulez-vous seulement changer la couleur du texte et rien d'autre? – Rich

+0

@Rich yup seulement la couleur du texte – DevC

Répondre

2

Si vous voulez seulement changer la couleur du texte il suffit d'utiliser l'autre méthode déléguée pickerView:attributedTitleForRow:forComponent:

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

    NSString *title; 
    UIFont *font; 
    if (row == 1) { 
     title = [NSString stringWithFormat:@" %@",[self.servicesArray objectAtIndex:row]]; 
     font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18]; 
    } else{ 
     title = [NSString stringWithFormat:@"  -%@",[self.servicesArray objectAtIndex:row] ]; 
     font = [UIFont fontWithName:kAppFont size:16]; 
    } 

    UIColor *textColor; 
    if (self.isBackgroundWhite) { 
     textColor = [UIColor blackColor]; 
    } else { 
     NSLog(@"Set white text"); 
     textColor = [UIColor whiteColor]; 
    } 

    NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; 
    paragraphStyle.alignment = NSTextAlignmentLeft; 

    NSDictionary *attributes = @{NSForegroundColorAttributeName : textColor, 
              NSFontAttributeName : font, 
            NSParagraphStyleAttributeName : paragraphStyle}; 

    return [[NSAttributedString alloc] initWithString:title attributes:attributes]; 
} 

une note également que vous devez appeler [self.pickerView reloadAllComponents]; lors d'un changement isBackgroundWhite. Je le ferais en remplaçant le setter pour backgroundWhite et lorsque la valeur booléenne change, rechargez la vue du sélecteur.

EDIT:
Il semble y avoir un « bug » (intentionnel ou non) dans iOS 7 (fonctionne très bien sur iOS 6) avec le réglage de la UIFont sur un NSAttributedString retourné par la méthode pickerView:attributedTitleForRow:forComponent:. Ainsi, alors que ce qui précède fonctionne pour la couleur du texte, la police ne change pas. Heureusement, vous pouvez contourner cela avec le code dans la question. Voir this answer pour plus d'informations.

+0

J'ai besoin que tous les titres soient alignés à gauche et non au centre? Y a-t-il un moyen de faire ça ici? – DevC

+1

@Gman oui, vous pouvez utiliser 'NSParagraphStyleAttributeName', je vais ajouter cela. – Rich

+0

@Gman extrait ['NSMutableParagraphStyle'] (https://developer.apple.com/library/ios/documentation/Cocoa/Reference/ApplicationKit/Classes/NSMutableParagraphStyle_Class/Reference/Reference.html#//apple_ref/occ/cl/NSMutableParagraphStyle) pour plus d'options à définir sur l'objet 'paragraphStyle' :) – Rich

Questions connexes