【问题标题】:How to change the UIPickerView text color?如何更改 UIPickerView 文本颜色?
【发布时间】:2018-01-25 20:47:56
【问题描述】:

文本的默认 UIPickerView 颜色是黑色。 Swift4 中的语言有一些更新。我找到了自己的解决方案并在下面回答。

【问题讨论】:

    标签: uipickerview swift4


    【解决方案1】:
    func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
        return NSAttributedString(string: data[row], attributes: [NSAttributedStringKey.foregroundColor: UIColor.yellow])        
    }
    

    【讨论】:

      【解决方案2】:

      你可以使用:

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

      这只会改变当前选定行的颜色。

      另请参阅:https://stackoverflow.com/a/9866686/5306470

      【讨论】:

      • 这只会在你为我移动选择器视图时改变颜色。
      【解决方案3】:

      为 Swift 5 更新:

      func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
          return NSAttributedString(string: parksPickerData[row], attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])
      }
      

      【讨论】:

        【解决方案4】:

        我有一个包含 PickerViews 和 DatePickers 的项目(在 TableViewCell 内),我不得不为每种情况使用不同的句子。

        对于 PickerViews:

        func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
                let titleData = "data"
                let myTitle = NSAttributedString(string: titleData, attributes: [NSAttributedString.Key.foregroundColor: UIColor.lightGray])
        
                return myTitle
            }
        

        对于日期选择器:

        datePicker.setValue(UIColor.lightGray, forKey: "textColor")
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-10-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多