日期选择控件NSDatePicker常作为输入日期的辅助选择工具,本文将简要介绍相关API及属性。
NSLog(@"LOG_isBezeled:%@", dateField.isBezeled ? @"YES" : @"NO");
NSLog(@"LOG_isBordered:%@", dateField.isBordered ? @"YES" : @"NO");
//background(未知原因,无效,知道的朋友留个言)
[dateField setDrawsBackground:YES];//绘制背景色
[dateField setBackgroundColor:[NSColor redColor]];
//文字颜色
[dateField setTextColor:[NSColor blueColor]];
//样式,常用的NSDatePickerStyleTextFieldAndStepper
[dateField setDatePickerStyle:NSDatePickerStyleClockAndCalendar];//图表样式
//[dateField setDatePickerStyle:NSDatePickerStyleTextField];//文本框样式
//[dateField setDatePickerStyle:NSDatePickerStyleTextFieldAndStepper];//文本框样式,但有步进调整
//控制显示元素,例如只显示年月、小时分钟等
//[dateField setDatePickerElements: NSDatePickerElementFlagYearMonth];
//[dateField setDatePickerElements:NSDatePickerElementFlagHourMinute];
//对应的日历,注意DatePickerStyle是NSDatePickerStyleClockAndCalendar才有
NSCalendar *calendar = [dateField calendar];
NSInteger hour;
NSInteger minute;
NSInteger second;
NSInteger nanosecond;
[calendar getHour:&hour minute:&minute second:&second nanosecond:&nanosecond fromDate:nil];
NSLog(@"LOG_%ld -- %ld", (long)hour, (long)minute);
//locale
NSLog(@"LOG_%@", dateField.locale);
//mode,设置datePicker提供一个日期或者日期范围,NSDatePickerModeRange可拖动多选日期
[dateField setDatePickerMode:NSDatePickerModeRange];//可拖动多选日期
//[dateField setDatePickerMode:NSDatePickerModeSingle];//单选
//当前选中的日期
NSLog(@"dateValue:%@", dateField.dateValue);
[dateField setDateValue:[NSDate date]];//datePickerMode为NSDatePickerModeSingle时只有一个日期,多选时返回起始时间和间隔
//timeInterval,NSDatePickerModeSingle返回0
NSLog(@"LOG_%f", dateField.timeInterval);
//限制输入的最大最小日期,nil表示不限制
[dateField setMinDate:nil];
[dateField setMaxDate:[NSDate date]];