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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19232817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:10:53  来源:igfitidea点击:

How do I change the color of the text in a UIPickerView under iOS 7?

iosobjective-cuiviewios7uipickerview

提问by Doug Smith

I'm aware of the pickerView:viewForRow:forComponent:reusingViewmethod, but when using the viewit passes in reusingView:how do I change it to use a different text color? If I use view.backgroundColor = [UIColor whiteColor];none of the views show up anymore.

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

回答by Jon

There is a function in the delegate method that is more elegant:

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

Objective-C:

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

If you want to change the selection bar colors as well, I found that I had to add 2 separate UIViewsto the view containing the UIPickerView, spaced 35 pts apart for a picker height of 180.

如果您还想更改选择栏颜色,我发现我必须向UIViews包含 的视图添加 2 个单独的UIPickerView, 间隔 35 pts 才能使选择器高度为 180。

Swift 3:

斯威夫特 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:

斯威夫特 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:

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

Remember when you use the method: You don't need to implement titleForRowInComponent()as it is never called when using attributedTitleForRow().

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

回答by Bordz

Original post here: can I change the font color of the datePicker in iOS7?

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

回答by Lancewise

  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]
  1. 转到故事板
  2. 选择 PickerView
  3. 转到身份检查器(第三个选项卡)
  4. 添加用户定义的运行时属性
  5. 键路径 = 文本颜色
  6. 类型 = 颜色
  7. 值 = [您选择的颜色]

screenshot

截屏

回答by Matt

in Xamarin, override the UIPickerModelView method GetAttributedTitle

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

回答by harsh_v

Swift 4 (Update to the accepted answer)

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

回答by logeshpalani98

It works for me

这个对我有用

pickerView.setValue(UIColor.yellow, forKeyPath: "textColor")

回答by R Thomas

I ran into the same problem with a pickerViewusing two components. My solution is similar to above with a few modifications. Because I am using two components it is necessary to pull from two different arrays.

我在使用两个组件的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;
}

回答by Bobby

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