iOS 日期选择器
这是一个轻量级且简单的控件,支持地区和日历标识符。提供了 iPhone 和 iPad 的示例代码,还可以使用弹出视图。
使用弹出视图
如何安装
手动
将以下源文件添加到您的项目中
CalendarView.h CalendarView.m NSDate+CalendarView.h NSDate+CalendarView.m NSString+CalendarView.h NSString+CalendarView.m
CocoaPods
pod 'Calendar-iOS'
如何使用
您可以在 Interface Builder 中添加视图并将其设置类为 CalendarView 或在代码中创建
CalendarView *calendarView = [[CalendarView alloc] initWithPosition:10.0 y:10.0]; [self.view addSubview:calendarView];
所以,到此结束,您应该能看到日历视图。
此组件是为 iPhone/iPod 分辨率创建的,对于 iPad,它也可以工作,但显示得非常小。如果需要,您可以在 CalendarView.m 中的静态常量中进行调整,未来可能会进行缩放。
此日历有几个模式
typedef NS_ENUM(NSInteger, CalendarMode) { CalendarModeDefault, CalendarModeMonthsAndYears, CalendarModeYears };
默认 - 有日期、月份和年份,用户可以通过滑动或捏合手势在日历中切换月份
月份和年份 - 显示的月份和年份
年份 - 只显示年份
还有一些外部方法模拟滑动行为,以便需要在不同的 UI 方面。但是,这些事件将以不同于滑动的事件类型进行记录。
还有一些显示选项
// Whether the currently selected date should be marked @property (nonatomic, assign) BOOL shouldMarkSelectedDate; // Whether today's date should be marked @property (nonatomic, assign) BOOL shouldMarkToday; // Whether the month and year headers should be shown @property (nonatomic, assign) BOOL shouldShowHeaders; // Preferred weekday start - (void)setPreferredWeekStartIndex:(NSInteger)index;
日期标记 - 默认行为是标记当前选定的日期而不是今天,但可以根据您的需求进行自定义。如果两个都标记并恰好在同一天,它将以当前选择的颜色显示,而不是今天的颜色。
标题 - 默认行为是显示标题,但也可以隐藏它们,在这种情况下,其他所有内容都将相应地向上移位(在设置需要显示的调用之后)。
首选周起始日 - 默认行为是星期一。确定左列是星期几。
如何使用地域和日历标识符
创建基于您地域和国情的日历非常简单且实用。
NSLocale *persianLocale = [NSLocale alloc]initWithLocaleIdentifier:@"fa-IR"]; [self.calendarView setLocale:persianLocale];
要使用 CalendarIdentifier,请使用此属性
[self.calendarView setCalendarIdentifier:NSCalendarIdentifierPersian];
如果将 useVeryShortWeekdaySymbols property 设置为 YES
,则使用非常短的星期名称
self.calendarView.useVeryShortWeekdaySymbols = YES;
如何选择日期范围
如果用户长按项目,可以选择日期。要选择日期范围结束点,长按结束日期。要获取日期范围,请使用
didSelectRangeForStartDate:endDate
代理方法,如下所示
- (void) didSelectRangeForStartDate:(NSDate *)startDate andEndDate:(NSDate *)endDate{ NSLog(@"%s: start date :%@ \n end date : %@",__PRETTY_FUNCTION__,startDate,endDate); }
如何处理日期更改事件
为此,您应该使用 CalendarViewDelegate 协议
@interface ViewController : UIViewController <CalendarViewDelegate> @end
并设置代理
self.calendarView.calendarDelegate = self;
之后,您应该实现必需的方法 didChangeCalendarDate
- (void)didChangeCalendarDate:(NSDate *)date { NSLog(@"didChangeCalendarDate:%@", date); }
有关更多详细信息,还有其他事物的可选方法
@optional - (void)didChangeCalendarDate:(NSDate *)date withType:(NSInteger)type withEvent:(NSInteger)event; - (void)didDoubleTapCalendar:(NSDate *)date withType:(NSInteger)type;
如何自定义颜色
对于颜色的自定义,您可以使用以下属性:
// Main color of numbers @property (nonatomic, strong) UIColor *fontColor; // Color of the headers (Year and month) @property (nonatomic, strong) UIColor *fontHeaderColor; // Color of selected numbers @property (nonatomic, strong) UIColor *fontSelectedColor; // Color of selection @property (nonatomic, strong) UIColor *selectionColor; // Color of today @property (nonatomic, strong) UIColor *todayColor;
例如:
self.calendarView.selectionColor = [UIColor colorWithRed:0.203 green:0.666 blue:0.862 alpha:1.000]; self.calendarView.fontHeaderColor = [UIColor colorWithRed:0.203 green:0.666 blue:0.862 alpha:1.000];
许可证
iOS日历遵守MIT许可证。更多详情请参阅LICENSE文件。