开源的日历控件FSCalendar
创建FSCalendar:
@property (nonatomic, strong) FSCalendar *calendar;
- (FSCalendar *)calendar {
if (!_calendar) {
_calendar = [[FSCalendar alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 248)]; _calendar.dataSource = self; _calendar.delegate = self; //设置翻页方式为水平
_calendar.scrollDirection = FSCalendarScrollDirectionHorizontal; //设置是否用户多选
_calendar.allowsMultipleSelection = NO; _calendar.appearance.caseOptions = FSCalendarCaseOptionsHeaderUsesUpperCase|FSCalendarCaseOptionsWeekdayUsesSingleUpperCase; //这个属性控制"上个月"和"下个月"标签在静止时刻的透明度 _calendar.appearance.headerMinimumDissolvedAlpha = 0; _calendar.backgroundColor = [ToolHelper colorWithHexString:@"#368de7"]; //设置周字体颜色
_calendar.appearance.weekdayTextColor = [UIColor whiteColor]; //设置头字体颜色
_calendar.appearance.headerTitleColor = [UIColor whiteColor]; //创建点击跳转显示上一月和下一月button
UIButton *previousButton = [UIButton buttonWithType:UIButtonTypeCustom]; previousButton.frame = CGRectMake(self.view.centerX - 50 - 6.5, 13, 6.5, 13); previousButton.titleLabel.font = [UIFont systemFontOfSize:15]; [previousButton setImage:[UIImage imageNamed:@"backicon"] forState:UIControlStateNormal]; [previousButton addTarget:self action:@selector(previousClicked:) forControlEvents:UIControlEventTouchUpInside]; [_calendar addSubview:previousButton]; UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeCustom]; nextButton.frame = CGRectMake(self.view.centerX + 50, 13, 6.5, 13); nextButton.titleLabel.font = [UIFont systemFontOfSize:15]; [nextButton setImage:[UIImage imageNamed:@"backicon"] forState:UIControlStateNormal]; nextButton.imageView.transform = CGAffineTransformMakeRotation(M_PI); [nextButton addTarget:self action:@selector(nextClicked:) forControlEvents:UIControlEventTouchUpInside]; [_calendar addSubview:nextButton]; //设置当天的字体颜色
_calendar.todayColor = COLOR_BLUE; }
return _calendar;
}
//上一月按钮点击事件- (void)previousClicked:(id)sender {
NSDate *currentMonth = self.calendar.currentPage;
NSDate *previousMonth = [self.calendar dateBySubstractingMonths:1 fromDate:currentMonth];
[self.calendar setCurrentPage:previousMonth animated:YES];
}//下一月按钮点击事件- (void)nextClicked:(id)sender {
NSDate *currentMonth = self.calendar.currentPage;
NSDate *nextMonth = [self.calendar dateByAddingMonths:1 toDate:currentMonth];
[self.calendar setCurrentPage:nextMonth animated:YES];
}
相关属性说明:
1._calendar.appearance.caseOptions
// 定义typedef NS_OPTIONS(NSUInteger, FSCalendarCaseOptions) {
// 月文字(如果手机系统字体为中文,那么以下两种选项设置任何一种都是中文显示;如果为英文则按照以下相应的设置生效) FSCalendarCaseOptionsHeaderUsesDefaultCase = 0, // June 2016 FSCalendarCaseOptionsHeaderUsesUpperCase = 1, // JUNE 2016 // 周文字 (如果手机系统为中文,设置则按中文生效,为英文则按照英文生效) FSCalendarCaseOptionsWeekdayUsesDefaultCase = 0 << 4, // Sun、Mon、Tue、Wed、Thu、Fri、Sat; 周日、周一、周二、周三、周四、周五、周六 FSCalendarCaseOptionsWeekdayUsesUpperCase = 1 << 4, // SUN、MON、TUE、WED、THU、FRI、SAT; 周日、周一、周二、周三、周四、周五、周六 FSCalendarCaseOptionsWeekdayUsesSingleUpperCase = 2 << 4, // S、M、T、W、T、F、S; 日、一、二、三、四、五、六};
FSCalendarAppearance:用于全局外观属性的定制。
FSCalendarDelegateAppearance:针对某天外观属性的定制。
#pragma mark FSCalendarDelegateAppearance//设置当前月与非当前月字体颜色- (UIColor *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance titleDefaultColorForDate:(NSDate *)date {
if ([calendar date:[NSDate date] sharesSameMonthWithDate:date]) {
return [UIColor whiteColor];
} else {
return [ToolHelper colorWithHexString:@"#8cc0f5"];//[UIColor colorWithWhite:0.702 alpha:1.000] }
}//设置选中日期与未选中日期Title的颜色- (UIColor *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance titleSelectionColorForDate:(NSDate *)date {
if (_dateArray.count > 0) {
for (NSDate *obj in _dateArray) {
if ([_calendar date:obj sharesSameDayWithDate:date]) {
return COLOR_BLUE;
}
}
}
return [UIColor whiteColor];
}//设置选中日期与未选中日期的填充色- (UIColor *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance selectionColorForDate:(NSDate *)date {
if (_dateArray.count > 0) {
for (NSDate *obj in _dateArray) {
if ([_calendar date:obj sharesSameDayWithDate:date]) {
return [UIColor whiteColor];
}
}
}
return COLOR_BLUE;
}//设置可选日期与不可选日期的border颜色- (UIColor *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance borderDefaultColorForDate:(NSDate *)date {
if (_dateArray.count > 0) {
for (NSDate *obj in _dateArray) {
if ([_calendar date:obj sharesSameDayWithDate:date]) {
return COLOR_YELLOW;
}
}
}
return COLOR_BLUE;
}//设置选中日期的border颜色- (UIColor *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance borderSelectionColorForDate:(NSDate *)date {
return [UIColor whiteColor];
}//设置可选日期- (BOOL)calendar:(FSCalendar *)calendar shouldSelectDate:(NSDate *)date {
if (_dateArray.count > 0) {
for (NSDate *obj in _dateArray) {
if ([_calendar date:obj sharesSameDayWithDate:date]) {
return YES;
}
}
}
return NO;
}
#pragma mark FSCalendarDelegate//选中某一天进行相关操作
- (void)calendar:(FSCalendar *)calendar didSelectDate:(NSDate *)date {
//...}
//取消选中的日期进行相关操作
- (void)calendar:(FSCalendar *)calendar didDeselectDate:(NSDate *)date {
//...}