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


117

我知道该pickerView:viewForRow:forComponent:reusingView方法,但是在使用时viewreusingView:如何将其更改为使用其他文本颜色?如果使用view.backgroundColor = [UIColor whiteColor];,则不再显示任何视图。



2
@AaronBrager。在此问题之后,询问您提供的链接是否重复。
Gajendra K Chauhan

Answers:


323

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

目标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()


到目前为止,这是最好的答案,因为它也适用于具有多个组件的选择器视图。
PKCLsoft

29

原始文章在这里:我可以在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;
}

该解决方案对于包含单个组件的pickerview效果很好。如果大于1,则标签将与我看到的重叠。
PKCLsoft

23
  1. 转到情节提要
  2. 选择PickerView
  3. 转到身份检查器(第三个标签)
  4. 添加用户定义的运行时属性
  5. KeyPath = textColor
  6. 类型=颜色
  7. 值= [您选择的颜色]

屏幕截图


请解释您的答案
Gwenc37 2014年

11
它工作了一半,cus文本为黑色,但是当您在选择器上滚动它时,他变成了白色
Shial 2014年

4
仅当您滚动时才有效
Chris Van Buskirk

嗨@ chris-van-buskirk检查我的附加组件[_thePrettyPicker selectRow:prettyIndex inComponent:0动画:是];
WINSergey

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

2

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])
    }
}

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;
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.