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

Dou*_*ith 115 objective-c uipickerview uiview ios ios7

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

Jon*_*Jon 316

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

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;
}
Run Code Online (Sandbox Code Playgroud)

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

斯威夫特3:

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

    let string = "myString"
    return NSAttributedString(string: string, attributes: [NSForegroundColorAttributeName:UIColor.white])
}
Run Code Online (Sandbox Code Playgroud)

斯威夫特4:

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

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

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])
}
Run Code Online (Sandbox Code Playgroud)

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

  • 惊人!!这是最好的,应该是选定的正确答案.谢谢. (2认同)
  • 小错字:NSAttributedString.Key.foregroundColor注意Key是大写的. (2认同)

Bor*_*rdz 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;
}
Run Code Online (Sandbox Code Playgroud)


小智 22

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

截图

  • 它的工作在一半,cus文本是黑色的,但是当你在选择器上滚动它时,他会变成白色 (11认同)
  • 仅在滚动时才有效 (4认同)

小智 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;
}
Run Code Online (Sandbox Code Playgroud)


log*_*i98 6

这个对我有用

pickerView.setValue(UIColor.yellow, forKeyPath: "textColor")
Run Code Online (Sandbox Code Playgroud)