如何在 iOS7下更改 UIPickerView 中文本的颜色?

我知道 pickerView:viewForRow:forComponent:reusingView方法,但是当使用 view时,它在 reusingView:中传递,我如何改变它使用不同的文本颜色?如果我使用 view.backgroundColor = [UIColor whiteColor];,就不会再显示任何视图。

78441 次浏览

原文链接: 我可以在 iOS7中更改 datePicker 的字体颜色吗?

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

委托方法中有一个函数更优雅:

目标 C:

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

如果你也想改变选择栏的颜色,我发现我必须添加2个独立的 UIViews到包含 UIPickerView的视图中,间隔35分,选择器高度为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])
}

迅捷4:

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {


let string = "myString"
return NSAttributedString(string: string, attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
}

迅捷4.2:

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {


let string = "myString"
return NSAttributedString(string: string, attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])
}

请记住当您使用该方法时: 您不需要实现 titleForRowInComponent(),因为在使用 attributedTitleForRow()时从不调用它。

我在使用两个组件的 pickerView时遇到了同样的问题。我的解决方案与上面类似,只是做了一些修改。因为我使用两个组件,所以有必要从两个不同的数组中提取。

- (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;
}
  1. 进入故事板
  2. 选择 PickerView
  3. Go to Identity inspector (3rd tab)
  4. 添加用户定义的运行时属性
  5. KeyPath = textColor
  6. 字体 = 颜色
  7. Value = [选择的颜色]

screenshot

在 Xamarin,重写 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;
}
- (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;
}

Swift 4(更新已接受的答案)

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.setValue(UIColor.yellow, forKeyPath: "textColor")