Skip to content

diligencefu/theDateSelector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

theDateSelector

常见的日历

之前没做过日历的时候感觉很难,做过一遍之后,发现其实并不是男,只是麻烦了些。因为我为了省事,有些东西只是简单的举一个例子什么的,比较懒。大家有什么需要延伸的功能都可以在里面找下对应的东西。话不多说,看吧。

#pragma mark -Private //返回当月天数

  • (NSUInteger)numberOfDaysInMonth:(NSDate *)date{ NSCalendar *greCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; [greCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]]; return [greCalendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date].length;

}

//返回当月第一天数据

  • (NSDate *)firstDateOfMonth:(NSDate *)date{ NSCalendar *greCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; [greCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]]; NSDateComponents *comps = [greCalendar components:NSCalendarUnitYear | NSCalendarUnitMonth |NSCalendarUnitWeekday | NSCalendarUnitDay fromDate:date]; comps.day = 1; return [greCalendar dateFromComponents:comps]; }

  • (NSUInteger)startDayOfWeek:(NSDate *)date { NSCalendar *greCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; [greCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];//Asia/Shanghai NSDateComponents *comps = [greCalendar components:NSCalendarUnitYear | NSCalendarUnitMonth |NSCalendarUnitWeekday | NSCalendarUnitDay fromDate:[self firstDateOfMonth:date]]; return comps.weekday; }

//上个月

  • (NSDate *)getLastMonth:(NSDate *)date{ NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; [greCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]]; NSDateComponents *comps = [greCalendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date]; comps.month -= 1; return [greCalendar dateFromComponents:comps]; } //下个月
  • (NSDate *)getNextMonth:(NSDate *)date{ NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; [greCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]]; NSDateComponents *comps = [greCalendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date]; comps.month += 1; return [greCalendar dateFromComponents:comps]; }

//目标日期

  • (NSDate *)getCurrentMonth:(NSDate *)date andYear:(NSInteger)year andMonth:(NSInteger)month andDay:(NSInteger)day{ NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; [greCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]]; NSDateComponents *comps = [greCalendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date]; comps.year = year; comps.month = month; comps.day = day; return [greCalendar dateFromComponents:comps]; }

//当前月

  • (NSDate *)getCurrentMonth:(NSDate *)date{ NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; [greCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]]; NSDateComponents *comps = [greCalendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date]; return [greCalendar dateFromComponents:comps]; }

//日期的当天

  • (NSDate *)dateOfDay:(NSInteger)day{ NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; [greCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]]; NSDateComponents *comps = [greCalendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:self.chooseDate]; comps.day = day; return [greCalendar dateFromComponents:comps]; }

@end

@implementation CalendarHeaderView

  • (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) {

      NSArray *weekArray = [[NSArray alloc] initWithObjects:@"日",@"一",@"二",@"三",@"四",@"五",@"六", nil];
    
      for (int i=0; i<weekArray.count; i++) {
          UILabel *weekLabel = [[UILabel alloc] initWithFrame:CGRectMake(i*Iphone6Scale(54), 0, Iphone6Scale(54), HeaderViewHeight)];
          weekLabel.textAlignment = NSTextAlignmentCenter;
          weekLabel.textColor = [UIColor grayColor];
          weekLabel.font = [UIFont systemFontOfSize:13.f];
          weekLabel.text = weekArray[i];
          [self addSubview:weekLabel];
      }
      
      UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, HeaderViewHeight, self.frame.size.width, 1)];
      lineView.backgroundColor = kRGBColor(0, 134, 237);
    
      [self addSubview:lineView];
    

    } return self; } @end

@implementation CalendarCell

  • (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { CGFloat width = self.contentView.frame.size.width0.6; CGFloat height = self.contentView.frame.size.height0.6;

      // 选中BG
      UIView* selectedBGView = [[UIView alloc] initWithFrame:CGRectMake( (self.contentView.width - self.contentView.height) / 2.0, 0, self.contentView.height, self.contentView.height)];
      selectedBGView.tag = 9588;
      selectedBGView.hidden = YES;
      selectedBGView.layer.cornerRadius = selectedBGView.bounds.size.width / 2.0;
      selectedBGView.backgroundColor = kRGBColor(24, 147, 239);
      
      
      UILabel *dayLabel = [[UILabel alloc] initWithFrame:CGRectMake( self.contentView.frame.size.width*0.5-width*0.5,  self.contentView.frame.size.height*0.5-height*0.5, width, height )];
      dayLabel.textAlignment = NSTextAlignmentCenter;
    
      dayLabel.font = [UIFont boldSystemFontOfSize:13];
      dayLabel.backgroundColor = [UIColor clearColor];
      
      [self.contentView addSubview:selectedBGView];
      [self.contentView addSubview:dayLabel];
    
      self.dayLabel = dayLabel;
    

    } return self; }

  • (void)setMonthModel:(MonthModel *)monthModel{ _monthModel = monthModel; self.dayLabel.text = [NSString stringWithFormat:@"%02ld",monthModel.dayValue];

    if (monthModel.isSelectedDay) { self.dayLabel.text = @"今天"; }

    if (monthModel.isCanClick) { self.dayLabel.textColor = [UIColor blackColor]; self.userInteractionEnabled = YES;

    } else { self.dayLabel.textColor = [UIColor lightGrayColor]; self.userInteractionEnabled = NO; } }

@end

@implementation MonthModel

About

常见的日历

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published