扩展 UIPickerView 以更改标题颜色
Extend UIPickerView to change title color
我希望应用程序中的所有 UIPickerView 都具有白色文本。我可以让它在单个 pickerview 上正常工作,但我希望它适用于所有事件。
我走到这一步...
extension UIPickerView {
func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
let titleData = ?????
let colorTitle = NSAttributedString(string: titleData, attributes: [NSForegroundColorAttributeName:UIColor.whiteColor()])
}
但是,我实际上并不想更改文本...只是更改颜色。如何获取指定行的标题(文本)以便我可以将其作为 NSAttributedString 返回?
谢谢。
我最终创建了 UIDatePicker 的子类并覆盖了 addSubView 方法。下面的代码对我来说效果很好。
class ColoredDatePicker: UIDatePicker {
var changed = false
override func addSubview(view: UIView) {
if !changed {
changed = true
self.setValue(UIColor.whiteColor(), forKey: "textColor")
}
super.addSubview(view)
}
}
我希望应用程序中的所有 UIPickerView 都具有白色文本。我可以让它在单个 pickerview 上正常工作,但我希望它适用于所有事件。
我走到这一步...
extension UIPickerView {
func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
let titleData = ?????
let colorTitle = NSAttributedString(string: titleData, attributes: [NSForegroundColorAttributeName:UIColor.whiteColor()])
}
但是,我实际上并不想更改文本...只是更改颜色。如何获取指定行的标题(文本)以便我可以将其作为 NSAttributedString 返回?
谢谢。
我最终创建了 UIDatePicker 的子类并覆盖了 addSubView 方法。下面的代码对我来说效果很好。
class ColoredDatePicker: UIDatePicker {
var changed = false
override func addSubview(view: UIView) {
if !changed {
changed = true
self.setValue(UIColor.whiteColor(), forKey: "textColor")
}
super.addSubview(view)
}
}