【问题标题】:How do I change the color of the text in a UIPickerView under iOS 7?如何在 iOS 7 下更改 UIPickerView 中文本的颜色?
【发布时间】:2013-10-14 11:56:15
【问题描述】:

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

【问题讨论】:

标签: ios objective-c uiview ios7 uipickerview


【解决方案1】:

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

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

如果您还想更改选择栏颜色,我发现我必须将 2 个单独的 UIViews 添加到包含 UIPickerView 的视图中,间隔 35 pts 以使选择器高度为 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() 时它永远不会被调用。

【讨论】:

  • 到目前为止,这是最好的答案,因为它也适用于具有多个组件的选择器视图。
【解决方案2】:

原帖在这里: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 个,标签将与我所看到的重叠。
【解决方案3】:
  1. 转到故事板
  2. 选择 PickerView
  3. 转到身份检查器(第三个选项卡)
  4. 添加用户定义的运行时属性
  5. KeyPath = textColor
  6. 类型 = 颜色
  7. 值 = [您选择的颜色]

【讨论】:

  • 请解释一下你的答案
  • 它工作了一半,cus 文本是黑色的,但是当你在选择器上滚动它时,它会变成白色
  • 仅在滚动时有效
  • 嗨 @chris-van-buskirk 检查我的插件 [_thePrettyPicker selectRow:prettyIndex inComponent:0 animated:YES];
【解决方案4】:

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

【讨论】:

    【解决方案5】:

    对我有用

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

    【讨论】:

      【解决方案6】:

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

      【讨论】:

        【解决方案7】:

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

        【讨论】:

          【解决方案8】:
          - (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;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-01-09
            • 1970-01-01
            • 1970-01-01
            • 2013-09-15
            • 1970-01-01
            • 2013-10-04
            相关资源
            最近更新 更多