Je suis conscient de la pickerView:viewForRow:forComponent:reusingView
méthode de pickerView:viewForRow:forComponent:reusingView
, mais lors de l'utilisation de la view
il passe dans reusingView:
comment puis-je le changer pour utiliser une couleur de text différente? Si j'utilise view.backgroundColor = [UIColor whiteColor];
aucune des vues n'apparaît plus.
Il y a une fonction dans la méthode délégué qui est plus élégante:
- (NSAtsortingbutedSsortingng *)pickerView:(UIPickerView *)pickerView atsortingbutedTitleForRow:(NSInteger)row forComponent:(NSInteger)component { NSSsortingng *title = @"sample title"; NSAtsortingbutedSsortingng *attSsortingng = [[NSAtsortingbutedSsortingng alloc] initWithSsortingng:title atsortingbutes:@{NSForegroundColorAtsortingbuteName:[UIColor whiteColor]}]; return attSsortingng; }
Si vous voulez aussi changer les colors de la barre de sélection, j'ai trouvé que je devais append 2 UIViews séparées à la vue contenant l'UIPickerView, espacées de 35 pts pour une hauteur de 180.
Mise à jour pour Swift 3:
func pickerView(_ pickerView: UIPickerView, atsortingbutedTitleForRow row: Int, forComponent component: Int) -> NSAtsortingbutedSsortingng? { let ssortingng = "mySsortingng" return NSAtsortingbutedSsortingng(ssortingng: ssortingng, atsortingbutes: [NSForegroundColorAtsortingbuteName:UIColor.white]) }
N'oubliez pas lorsque vous utilisez la méthode: Vous n'avez pas besoin d'implémenter titleForRowInComponent()
car il n'est jamais appelé lors de l'utilisation de atsortingbutedTitleForRow()
.
message original [ici] ( puis-je changer la couleur de la police de la datePicker dans iOS7? )
- (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 grayColor]; label.textColor = [UIColor whiteColor]; label.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18]; label.text = [NSSsortingng ssortingngWithFormat:@" %d", row+1]; return label; } // number Of Components - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 1; } // number Of Rows In Component - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component { return 6; }
1. Go to storyboard 2. Select PickerView 3. Go to Identity inspector (3rd tab) 4. Add User Defined Runtime Atsortingbute 5. KeyPath = textColor 6. Type = Color 7. Value = [Color of you choice]
dans Xamarin, remplacez la méthode UIPickerModelView GetAtsortingbutedTitle
public override NSAtsortingbutedSsortingng GetAtsortingbutedTitle(UIPickerView picker, nint row, nint component) { // change text white ssortingng title = GetTitle (picker, row, component); // get current text from UIPickerViewModel::GetTitle NSAtsortingbutedSsortingng ns = new NSAtsortingbutedSsortingng (title, null, UIColor.White); // null = font, use current font return ns; }
J'ai rencontré le même problème avec un pickerView utilisant deux composants. Ma solution est similaire à ci-dessus avec quelques modifications. Parce que j'utilise deux composants, il est nécessaire de tirer à partir de deux arrays différents.
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{ UILabel *label = [[UILabel alloc] init]; label.backgroundColor = [UIColor blueColor]; label.textColor = [UIColor whiteColor]; label.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18]; //WithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 60)]; if(component == 0) { label.text = [countryArray objectAtIndex:row]; } else { label.text = [cityArray objectAtIndex:row]; } return label; }
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { UILabel* pickerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 37)]; pickerLabel.text = @"text"; pickerLabel.textColor = [UIColor redColor]; return pickerLabel; }