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

问题描述 投票:111回答:7

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

ios objective-c uiview ios7 uipickerview
7个回答
308
投票

委托方法中有一个更优雅的功能:

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

如果你想改变选择条颜色,我发现我必须在包含UIViews的视图中添加2个单独的UIPickerView,间隔35磅,拾取器高度为180。

斯威夫特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])
}

Swift 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()时它从未被调用过。


27
投票

原帖这里:can I change the font color of the datePicker in 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;
}

20
投票
  1. 去故事板
  2. 选择PickerView
  3. 转到身份检查器(第3个选项卡)
  4. 添加用户定义的运行时属性
  5. KeyPath = textColor
  6. Type = Color
  7. 值= [您选择的颜色]


7
投票

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

2
投票

我使用两个组件使用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
投票
- (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
投票

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])
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.