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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在iOS 7下更改UIPickerView中文本的颜色?相关的知识,希望对你有一定的参考价值。

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

答案

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

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

另一答案

原帖这里: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;
}
另一答案
  1. 去故事板
  2. 选择PickerView
  3. 转到身份检查器(第3个选项卡)
  4. 添加用户定义的运行时属性
  5. KeyPath = textColor
  6. Type = Color
  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;
}
另一答案

我使用两个组件使用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;
}
另一答案
- (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])
    }
}

以上是关于如何在iOS 7下更改UIPickerView中文本的颜色?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 iOS 14 中更改 UIPickerView 选择的色调颜色?

如何在 iOS 应用程序中通过 UIPickerView 实现视图之间的转换?

PhoneGap:iOS 7 中的 UIPickerView 不会像在 iOS 6 中那样自动调整字体大小。关于如何在 iOS 7 中实现的任何解决方案?

在 iOS 7/Objective-C 中,如何以编程方式将 UIPickerView 捕捉到它所在的父视图的底部?

如何更改 UIpickerView 上的元素按下按钮或按下另一个按钮

如何在目标 C 中更改 UIPickerView 的宽度