Changing text color of datepicker

前端 未结 8 2342
鱼传尺愫
鱼传尺愫 2020-12-01 15:57

Is it any way to change text color of datepicker in iOS8? I\'ve that it isn\'t possible in iOS7 and prior, something changed in 8th version?

For example I\'ve found

相关标签:
8条回答
  • 2020-12-01 16:15

    This worked for me:

    setting text color for all date picker subviews

    for view in datePicker.subviews {
        view.setValue(UIColor.white, forKeyPath: "textColor")
    }
    

    Possible duplicate: Set text color and font for UIDatePicker in iOS8/Swift

    0 讨论(0)
  • 2020-12-01 16:16

    Resolving the issue with black "Today" via highlightsToday set to "false" has a side-effect - it's no longer has "Today" label in the list.

    I've found a different solution here which works perfect without any side-effects: Set text color and font for UIDatePicker in iOS8/Swift

    datePicker.setValue(UIColor.whiteColor(), forKeyPath: "textColor")
    datePicker.datePickerMode = .CountDownTimer
    datePicker.datePickerMode = .DateAndTime //or whatever your original mode was
    

    Looks like a dirty hack but that's we have at the moment: changing datePickerMode re-draws it correctly. So just set it to something different from your desired mode and change it back to original.

    0 讨论(0)
  • 2020-12-01 16:19

    Still Works, Swift 4 Update

    datePicker.setValue(UIColor.white, forKeyPath: "textColor")
    

    This works for me.

    datePicker.setValue(UIColor.whiteColor(), forKeyPath: "textColor")
    
    0 讨论(0)
  • 2020-12-01 16:32

    If you don't want the selected text to be black at start you need to set both textColor and highlightsToday :

    0 讨论(0)
  • 2020-12-01 16:34

    Found solution at comments of stackoverflow. If you need just to change text color to yours and assign this subclass you your picker. For whiteColor works as magic.

    Only minus i've found that color two lines of selected number is still gray.

    class ColoredDatePicker: UIDatePicker {
        var changed = false
        override func addSubview(view: UIView) {
            if !changed {
                changed = true
                self.setValue(UIColor.whiteColor(), forKey: "textColor")
            }
            super.addSubview(view)
        }
    }
    
    0 讨论(0)
  • 2020-12-01 16:37

    It's working on iOS 10 only if you set both properties.

    datePicker.setValue(UIColor.white, forKeyPath: "textColor")
    datePicker.setValue(false, forKeyPath: "highlightsToday")
    
    0 讨论(0)
提交回复
热议问题