Set text color and font for UIDatePicker in iOS8/Swift

后端 未结 16 2186
囚心锁ツ
囚心锁ツ 2020-12-03 21:02

I\'ve been having trouble trying to set the UIDatePicker font and color. Everything else in my app was fairly straightforward to adjust except

相关标签:
16条回答
  • 2020-12-03 21:19

    you just need to set 2 lines of code in viewdidLoad / viewWillAppear accoding where you using DatePicker.

    dobDatePicker.setValue(UIColor.whiteColor(), forKeyPath: "textColor")
    dobDatePicker.setValue(false, forKey: "highlightsToday")
    

    See the Result like this:

    0 讨论(0)
  • 2020-12-03 21:20

    I believe this is the definitive solution for countdown timers.
    It's an expansion of yildirimosman's answer.

    //text color
    datePicker.setValue(UIColor.whiteColor(), forKey: "textColor")
    //picker background
    datePicker.subviews[0].subviews[0].backgroundColor = UIColor.clearColor() //the picker's own background view
    //dividers
    datePicker.subviews[0].subviews[1].backgroundColor = UIColor.whiteColor()
    datePicker.subviews[0].subviews[2].backgroundColor = UIColor.whiteColor()
    //labels: "hours" and "min"
    datePicker.subviews[0].subviews[3].setValue(UIColor.lightGrayColor(), forKey: "textColor")
    datePicker.subviews[0].subviews[4].setValue(UIColor.lightGrayColor(), forKey: "textColor")
    //refresh the tableview (to force initial row textColor to change to white)
    datePicker.subviews[0].setNeedsLayout()
    datePicker.subviews[0].layoutIfNeeded()
    
    0 讨论(0)
  • 2020-12-03 21:24

    you can use

    datePicker.setValue(UIColor.whiteColor(), forKey: "textColor")
    datePicker.setValue(false, forKey: "highlightsToday")
    //for selector color
    datePickerView.subviews[0].subviews[1].backgroundColor = UIColor.whiteColor()
    datePickerView.subviews[0].subviews[2].backgroundColor = UIColor.whiteColor()
    
    0 讨论(0)
  • 2020-12-03 21:25

    The API does not provide a way to do this. You can make a pretty convincing replica yourself using a UIPickerView rather than using UIDatePicker. Se here

    0 讨论(0)
  • 2020-12-03 21:27

    Adding textColor and highlightsToday to user defined variables did the trick for me :

    0 讨论(0)
  • 2020-12-03 21:27

    I wanted to do this but set it for when someone has the date set prior to now, or after now. I had to reload the data, but when I did it ended up setting it to the current DateTime when using the above example.

    So what I did was set a temporary value and then set it after the reload. It does make it do an animated effect, but it works. If you know a better way, let me know...

    func dateChanged(sender: UIDatePicker) {
        print(sender.date.description)
    
        let tempDate = sender.date
        let currentDate = NSDate()
    
        if originalDate.isLessThanDate(currentDate) {
            originalDate = sender.date
            if sender.date.isGreaterThanDate(currentDate) {
                sender.setValue(UIColor.blackColor(), forKeyPath: "textColor")
                sender.datePickerMode = .CountDownTimer
                sender.datePickerMode = .DateAndTime
                sender.date = tempDate
                sender.reloadInputViews()
            }
        }
    
        if sender.date.isLessThanDate(currentDate) {
            sender.setValue(UIColor.redColor(), forKeyPath: "textColor")
            sender.datePickerMode = .CountDownTimer
            sender.datePickerMode = .DateAndTime
            sender.date = tempDate
            sender.reloadInputViews()
        }
    }
    
    0 讨论(0)
提交回复
热议问题