JTCalendar是一个可自定义性高,功能强大的日历类库,我们可以改变日历样式,选中日期的多选和单选等功能,下面将介绍JTCalendar的基本使用方法;
附上git地址:https://github.com/jie0394/JTCalendarTest.git
在你添加日历控件的ViewController中
_calendarManager.dateHelper.calendar.firstWeekday = 2;//设置第一列为周一
[_calendarManager setMenuView:_calendarMenuView];
[_calendarManager setContentView:_calendarContentView];
[_calendarManager setDate:[NSDate date]];
改变日历样式
- (void)calendar:(JTCalendarManager *)calendar prepareDayView:(JTCalendarDayView *)dayView{
if([dayView isFromAnotherMonth]){//是否显示上个月和下个月的日期
// dayView.hidden = YES;
dayView.circleView.hidden = YES;
dayView.backgroundColor = [UIColor whiteColor];
dayView.labNumber.hidden = YES;
dayView.textLabel.textColor = [UIColor colorWithHexString:@"CCCCCC"];
}
dayView.backgroundColor = [UIColor whiteColor];//每一日期格子的view;
dayView.textLabel.textColor = [UIColor colorWithHexString:@"CCCCCC"];//日期数字label,课改变字体颜色,大小等
if([_calendarManager.dateHelper date:[NSDate date] isTheSameDayThan:dayView.date]){//设置今天的日期样式,比如显示为今天,改变字体颜色
dayView.circleView.hidden = YES;//是否显示小圆点
dayView.textLabel.text = @"今天";
dayView.textLabel.textColor = [UIColor colorWithHexString:@"13C56D"];
dayView.labNumber.textColor = [UIColor colorWithHexString:@"666666"];
}
if (_dateSelected && [_calendarManager.dateHelper date:_dateSelected isTheSameDayThan:dayView.date]) {//选中日期
}
if([self haveEventForDay:dayView.date]){//当前日期是否有事件,我舍弃了小圆点,自定义显示了有几条信息
dayView.dotView.hidden = NO;
}
else{
dayView.dotView.hidden = YES;
}
if ([_calendarManager.dateHelper date:[NSDate date] isEqualOrAfter:dayView.date]
&& ![_calendarManager.dateHelper date:[NSDate date] isTheSameDayThan:dayView.date]) {//今天之前日期(改变字体颜色,等等,我们的需求是要改)
}
}
切换到上一页下一页所执行的代理事件
- (void)calendarDidLoadNextPage:(JTCalendarManager *)calendar//下个月
{
// NSLog(@"Next page loaded");
//[self loadData:[calendar date] andChangePage:YES];//从服务器请求下个月的事件
//[self refreshCalendar:[calendar date]];
}
- (void)calendarDidLoadPreviousPage:(JTCalendarManager *)calendar//上个月
{
// NSLog(@"Previous page loaded");
}
- (void)calendar:(JTCalendarManager *)calendar didTouchDayView:(JTCalendarDayView *)dayView
{
_dateSelected = dayView.date;
[self loadClassList:dayView.date];
// Animation for the circleView
dayView.circleView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.1, 0.1);
[UIView transitionWithView:dayView
duration:.3
options:0
animations:^{
dayView.circleView.transform = CGAffineTransformIdentity;
[_calendarManager reload];
} completion:nil];
// Don't change page in week mode because block the selection of days in first and last weeks of the month
if(_calendarManager.settings.weekModeEnabled){
return;
}
// Load the previous or next page if touch a day from another month
if(![_calendarManager.dateHelper date:_calendarContentView.date isTheSameMonthThan:dayView.date]){
if([_calendarContentView.date compare:dayView.date] == NSOrderedAscending){
[_calendarContentView loadNextPageWithAnimation];
}
else{
[_calendarContentView loadPreviousPageWithAnimation];
}
}
}
我们可以在JTCalendarPageView.m中做一些改动,然后会日历会自动隐藏和显示最后一行
找到reload这个方法,将
_numberOfWeeksDisplayed = MIN([_manager.dateHelper numberOfWeeks:_date], MAX_WEEKS_BY_MONTH);
if(_numberOfWeeksDisplayed == 0){
_numberOfWeeksDisplayed = [_manager.dateHelper numberOfWeeks:_date];
}
改成
_numberOfWeeksDisplayed = MIN(0, MAX_WEEKS_BY_MONTH);
if(_numberOfWeeksDisplayed == 0){
_numberOfWeeksDisplayed = [_manager.dateHelper numberOfWeeks:_date];
}
然后在layoutSubviews方法中加上这句话
- (void)layoutSubviews
{
[super layoutSubviews];
if(!_weeksViews){
return;
}
float hight = 0.0;
if ([_manager.dateHelper numberOfWeeks:self.manager.date] == 5) {
hight = (Winth - 20)/7 * 6;
}else{
hight = Winth - 20;
}
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, (Winth - 20), hight);
CGFloat y = 0;
CGFloat weekWidth = self.frame.size.width;
if(_manager.settings.pageViewHaveWeekDaysView){
CGFloat weekDayHeight = _weekDayView.frame.size.height; // Force use default height
if(weekDayHeight == 0){ // Or use the same height than weeksViews
weekDayHeight = self.frame.size.height / (_numberOfWeeksDisplayed + 1);
}
_weekDayView.frame = CGRectMake(0, 0, weekWidth, weekDayHeight);
y = weekDayHeight;
}
CGFloat weekHeight = (self.frame.size.height - y) / [_manager.dateHelper numberOfWeeks:self.manager.date];
for(UIView *weekView in _weeksViews){
weekView.frame = CGRectMake(0, y, weekWidth, weekHeight);
y += weekHeight;
}
}
继续回到viewContoller
添加下面几个方法
-(void)refreshCalendar:(NSDate *)currentDate{
//底部视图到日历的位置(用于遮盖日历最后一行,试了一下改变高度,发现改变高度每滑动一次内存就会增加很多,变得很卡,所以用了个很笨的方法)
float hight = 0.0; if ([self.calendarManager.dateHelper numberOfWeeks:currentDate] == 5) { hight = (Winth - 20)/7 ; if (Winth < 375) { hight = 0.8*(Winth - 20)/7; } self.contentViewTop.constant = 0 - hight;//使用autolayOut拖出来的底部视图距离顶部的日历控件的高度 }else{ self.contentViewTop.constant = 0; } }
再上一页和下一页加载时调用
- (void)calendarDidLoadNextPage:(JTCalendarManager *)calendar
{
// NSLog(@"Next page loaded");
[self refreshCalendar:[calendar date]];
}
- (void)calendarDidLoadPreviousPage:(JTCalendarManager *)calendar
{
// NSLog(@"Previous page loaded");
[self refreshCalendar:[calendar date]];
}
写的太乱了,没办法,老大让写文档,随便写写,如果有疑问可以问我,等有时间写个demo出来