前言:

UIDatePicker是比较常用的一个控件了,但是苹果却只提供极少的属性可以让我们使用,所以实际运用中难免要自定义。这里我提供俩种方法来改变UIDatePicker的显示字体颜色。显示效果如下:


屏幕快照 2017-12-07 下午11.06.45.png


首先是第一种方法:runtime

我们知道,runtime可以遍历一个对象的所有属性,虽然苹果没有公开UIDatePicker的字体颜色,但是我们可以遍历查找对应的属性名,并加以修改就可以实现了。
关键代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//MARK:runtime遍历所有属性名,并加以修改
func setDateTextColor(picker:UIDatePicker){
var count:UInt32 = 0
let propertys = class_copyPropertyList(UIDatePicker.self, &count)
for index in 0..<count {
let i = Int(index)
let property = propertys![i]
let propertyName = property_getName(property)

let strName = String.init(cString: propertyName, encoding: String.Encoding.utf8)
if strName == "textColor"{
picker.setValue(UIColor.red, forKey: strName!)
}
}
}

第二种方法:自定义UIPickerView

相比而言,UIPickerView可以使用比较多的属性进行操作,所以我们不妨曲线救国,使用UIPickerView来实现UIDatePicker的功能。此方法比较繁琐,但是可以高度自定义。

具体思路:

  • 准备3个数组存放年,月,日的数据,其中日的数组要根据相应的年和月来处理不同情况,月份还有闰年都会影响到,所以这一步需要小心计算。
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    /// 计算每个月的天数
    fileprivate func daysCount(year:Int,month:Int) -> Int{
    let isrunNian = year%4 == 0 ? (year%100 == 0 ? (year%400 == 0 ? true:false):true):false
    if month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12{
    self.setDayArr(num: 31)
    return 31
    }else if month == 4 || month == 6 || month == 9 || month == 11{
    self.setDayArr(num: 30)
    return 30
    }else if month == 2{
    if isrunNian{
    self.setDayArr(num: 29)
    return 29
    }else{
    self.setDayArr(num: 28)
    return 28
    }
    }
    return 0
    }
  • 数据源准备好后,就开始实现UIPickerView的代理方法吧,切换年或者月份的时候会影响到日这一列的数据。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if component == 0 {
yearIndex = row
}
if component == 1 {
monthIndex = row
}
if component == 2 {
dayIndex = row
}
if component == 0 || component == 1{
let selctYear = (yearArray![yearIndex!] as! NSString).intValue
let selctMonth = (monthArray![monthIndex!] as! NSString).intValue
let _ = self.daysCount(year: Int(selctYear) , month: Int(selctMonth))

if (dayArray?.count)!-1 < dayIndex! {
dayIndex = (dayArray?.count)!-1
}
}
pickerView.reloadAllComponents()
}
  • 最后别忘了我们的初衷,就是自定义显示字体颜色。重写
    pickerView viewForRow: forComponent:(NSInteger)component reusingView:方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let headLabel = UILabel.init()
headLabel.textColor = UIColor.red
headLabel.textAlignment = NSTextAlignment.center
if component == 0 {
headLabel.text = yearArray?[row] as? String
}
if component == 1 {
headLabel.text = monthArray?[row] as? String
}
if component == 2 {
headLabel.text = dayArray?[row] as? String
}
return headLabel
}

这样就大功告成了。


结语:Swift和OC在使用上还是有不少区别的,我刚用的时候也踩了不少坑,不过对于数据类型的使用确实比较严谨,感觉程序应该会更健壮了。

github项目地址: https://github.com/wang-qiuyu/CustomDatePicker