【发布时间】:2018-01-25 20:47:56
【问题描述】:
文本的默认 UIPickerView 颜色是黑色。 Swift4 中的语言有一些更新。我找到了自己的解决方案并在下面回答。
【问题讨论】:
标签: uipickerview swift4
文本的默认 UIPickerView 颜色是黑色。 Swift4 中的语言有一些更新。我找到了自己的解决方案并在下面回答。
【问题讨论】:
标签: uipickerview swift4
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
return NSAttributedString(string: data[row], attributes: [NSAttributedStringKey.foregroundColor: UIColor.yellow])
}
【讨论】:
你可以使用:
pickerView.setValue(UIColor.yellow, forKeyPath: "textColor")
这只会改变当前选定行的颜色。
【讨论】:
为 Swift 5 更新:
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
return NSAttributedString(string: parksPickerData[row], attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])
}
【讨论】:
我有一个包含 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")
【讨论】: