ios objective-c uiview ios7 uipickerview

¿Cómo cambio el color del texto en UIPickerView en iOS 7?



objective-c uiview (8)

Swift 4 (Actualización a la respuesta aceptada)

extension MyViewController: UIPickerViewDelegate{ } func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? { return NSAttributedString(string: "your-title-goes-here", attributes: [NSAttributedStringKey.foregroundColor: UIColor.white]) } }

pickerView:viewForRow:forComponent:reusingView método pickerView:viewForRow:forComponent:reusingView , pero cuando uso la view pasa al reusingView: ¿cómo lo cambio para usar un color de texto diferente? Si utilizo view.backgroundColor = [UIColor whiteColor]; ninguna de las vistas aparece más.


Esto funcionará:

- (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 = [NSString stringWithFormat:@" %d", row+1]; return label; } // total Components - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 1; } // total rows In Component - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component { return 6; }


Hay una función en el método de delegado que es más elegante:

C objetivo:

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component { NSString *title = @"sample title"; NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}]; return attString; }

Si también desea cambiar los colores de la barra de selección, descubrí que tenía que agregar 2 UIViews separados a la vista que contenía UIPickerView , separados por 35 pts para una altura de selector de 180.

Swift 3:

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? { let string = "myString" return NSAttributedString(string: string, attributes: [NSForegroundColorAttributeName:UIColor.white]) }

Swift 4:

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? { let string = "myString" return NSAttributedString(string: string, attributes: [NSAttributedStringKey.foregroundColor: UIColor.white]) }

Recuerde cuando usa el método: No necesita implementar titleForRowInComponent() ya que nunca se llama cuando se usa attributedTitleForRow() .


Me encontré con el mismo problema con un selector que usa dos componentes. Mi solución es similar a la anterior con algunas modificaciones. Debido a que estoy usando dos componentes, es necesario extraer de dos matrices diferentes.

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


en Xamarin, anule el método UIPickerModelView GetAttributedTitle

public override NSAttributedString GetAttributedTitle(UIPickerView picker, nint row, nint component) { // change text white string title = GetTitle (picker, row, component); // get current text from UIPickerViewModel::GetTitle NSAttributedString ns = new NSAttributedString (title, null, UIColor.White); // null = font, use current font return ns; }


publicación original [aquí] ( ¿puedo cambiar el color de fuente de datePicker en 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 = [NSString stringWithFormat:@" %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; }


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


1. Go to storyboard 2. Select PickerView 3. Go to Identity inspector (3rd tab) 4. Add User Defined Runtime Attribute 5. KeyPath = textColor 6. Type = Color 7. Value = [Color of you choice]