├── .gitignore ├── FDCalendar.podspec ├── FDCalendar ├── FDCalendar.h ├── FDCalendar.m ├── FDCalendarItem.h └── FDCalendarItem.m ├── FDCalendarDemo ├── FDCalendarDemo.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ └── contents.xcworkspacedata ├── FDCalendarDemo │ ├── AppDelegate.h │ ├── AppDelegate.m │ ├── Base.lproj │ │ ├── LaunchScreen.xib │ │ └── Main.storyboard │ ├── FDCalendar.h │ ├── FDCalendar.m │ ├── FDCalendarItem.h │ ├── FDCalendarItem.m │ ├── Images.xcassets │ │ └── AppIcon.appiconset │ │ │ └── Contents.json │ ├── Info.plist │ ├── ViewController.h │ ├── ViewController.m │ └── main.m ├── FDCalendarDemoTests │ ├── FDCalendarDemoTests.m │ └── Info.plist └── Resource │ ├── icon_next@2x.png │ └── icon_previous@2x.png ├── LICENSE ├── README.md ├── ScreenShot ├── screenshot1.png ├── screenshot2.png └── screenshot3.png └── preview.gif /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | build/ 4 | *.pbxuser 5 | !default.pbxuser 6 | *.mode1v3 7 | !default.mode1v3 8 | *.mode2v3 9 | !default.mode2v3 10 | *.perspectivev3 11 | !default.perspectivev3 12 | xcuserdata 13 | *.xccheckout 14 | *.moved-aside 15 | DerivedData 16 | *.hmap 17 | *.ipa 18 | *.xcuserstate 19 | *.DS_Store 20 | 21 | # CocoaPods 22 | # 23 | # We recommend against adding the Pods directory to your .gitignore. However 24 | # you should judge for yourself, the pros and cons are mentioned at: 25 | # http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control 26 | # 27 | #Pods/ 28 | -------------------------------------------------------------------------------- /FDCalendar.podspec: -------------------------------------------------------------------------------- 1 | # 2 | # Be sure to run `pod spec lint FDCalendar.podspec' to ensure this is a 3 | # valid spec and to remove all comments including this before submitting the spec. 4 | # 5 | # To learn more about Podspec attributes see http://docs.cocoapods.org/specification.html 6 | # To see working Podspecs in the CocoaPods repo see https://github.com/CocoaPods/Specs/ 7 | # 8 | 9 | Pod::Spec.new do |s| 10 | 11 | # ――― Spec Metadata ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 12 | # 13 | # These will help people to find your library, and whilst it 14 | # can feel like a chore to fill in it's definitely to your advantage. The 15 | # summary should be tweet-length, and the description more in depth. 16 | # 17 | 18 | s.name = "FDCalendar" 19 | s.version = "1.0.0" 20 | s.summary = "A custom calendar control in iOS." 21 | 22 | s.description = <<-DESC 23 | FDCalendar is a custom calendar control which includes world and chinese calendar date. It supports scrolling by left and right to jump to previous or next month date, also it can jump to the date which is selected. Otherwise it seems beautiful and good using. If you want to change some style of it, you need to fork it and modify the properties by yourself. If you think it is helpful, please star it. 24 | 25 | * Think: Why did you write this? What is the focus? What does it do? 26 | * CocoaPods will be using this to generate tags, and improve search results. 27 | * Try to keep it short, snappy and to the point. 28 | * Finally, don't worry about the indent, CocoaPods strips it! 29 | DESC 30 | 31 | s.homepage = "https://github.com/fergusding/FDCalendar" 32 | # s.screenshots = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif" 33 | 34 | 35 | # ――― Spec License ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 36 | # 37 | # Licensing your code is important. See http://choosealicense.com for more info. 38 | # CocoaPods will detect a license file if there is a named LICENSE* 39 | # Popular ones are 'MIT', 'BSD' and 'Apache License, Version 2.0'. 40 | # 41 | 42 | s.license = "MIT" 43 | # s.license = { :type => "MIT", :file => "FILE_LICENSE" } 44 | 45 | 46 | # ――― Author Metadata ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 47 | # 48 | # Specify the authors of the library, with email addresses. Email addresses 49 | # of the authors are extracted from the SCM log. E.g. $ git log. CocoaPods also 50 | # accepts just a name if you'd rather not provide an email address. 51 | # 52 | # Specify a social_media_url where others can refer to, for example a twitter 53 | # profile URL. 54 | # 55 | 56 | s.author = { "Fergus Ding" => "fergusding@augmentum.com.cn" } 57 | # Or just: s.author = "Fergus Ding" 58 | # s.authors = { "Fergus Ding" => "fergusding@augmentum.com.cn" } 59 | # s.social_media_url = "http://twitter.com/Fergus Ding" 60 | 61 | # ――― Platform Specifics ――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 62 | # 63 | # If this Pod runs only on iOS or OS X, then specify the platform and 64 | # the deployment target. You can optionally include the target after the platform. 65 | # 66 | 67 | # s.platform = :ios 68 | s.platform = :ios, "7.0" 69 | 70 | # When using multiple platforms 71 | # s.ios.deployment_target = "5.0" 72 | # s.osx.deployment_target = "10.7" 73 | 74 | 75 | # ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 76 | # 77 | # Specify the location from where the source should be retrieved. 78 | # Supports git, hg, bzr, svn and HTTP. 79 | # 80 | 81 | s.source = { :git => "https://github.com/fergusding/FDCalendar.git", :tag => "1.0.0" } 82 | 83 | 84 | # ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 85 | # 86 | # CocoaPods is smart about how it includes source code. For source files 87 | # giving a folder will include any h, m, mm, c & cpp files. For header 88 | # files it will include any header in the folder. 89 | # Not including the public_header_files will make all headers public. 90 | # 91 | 92 | s.source_files = "FDCalendar/*" 93 | # s.exclude_files = "Classes/Exclude" 94 | 95 | # s.public_header_files = "Classes/**/*.h" 96 | 97 | 98 | # ――― Resources ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 99 | # 100 | # A list of resources included with the Pod. These are copied into the 101 | # target bundle with a build phase script. Anything else will be cleaned. 102 | # You can preserve files from being cleaned, please don't preserve 103 | # non-essential files like tests, examples and documentation. 104 | # 105 | 106 | # s.resource = "icon.png" 107 | # s.resources = "Resources/*.png" 108 | 109 | # s.preserve_paths = "FilesToSave", "MoreFilesToSave" 110 | 111 | 112 | # ――― Project Linking ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 113 | # 114 | # Link your library with frameworks, or libraries. Libraries do not include 115 | # the lib prefix of their name. 116 | # 117 | 118 | # s.framework = "SomeFramework" 119 | s.frameworks = "Foundation", "UIKit" 120 | 121 | # s.library = "iconv" 122 | # s.libraries = "iconv", "xml2" 123 | 124 | 125 | # ――― Project Settings ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 126 | # 127 | # If your library depends on compiler flags you can set them in the xcconfig hash 128 | # where they will only apply to your library. If you depend on other Podspecs 129 | # you can include multiple dependencies to ensure it works. 130 | 131 | s.requires_arc = true 132 | 133 | # s.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" } 134 | # s.dependency "JSONKit", "~> 1.4" 135 | 136 | end 137 | -------------------------------------------------------------------------------- /FDCalendar/FDCalendar.h: -------------------------------------------------------------------------------- 1 | // 2 | // FDCalendar.h 3 | // FDCalendarDemo 4 | // 5 | // Created by fergusding on 15/8/20. 6 | // Copyright (c) 2015年 fergusding. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface FDCalendar : UIView 12 | 13 | - (instancetype)initWithCurrentDate:(NSDate *)date; 14 | 15 | @end 16 | -------------------------------------------------------------------------------- /FDCalendar/FDCalendar.m: -------------------------------------------------------------------------------- 1 | // 2 | // FDCalendar.m 3 | // FDCalendarDemo 4 | // 5 | // Created by fergusding on 15/8/20. 6 | // Copyright (c) 2015年 fergusding. All rights reserved. 7 | // 8 | 9 | #import "FDCalendar.h" 10 | #import "FDCalendarItem.h" 11 | 12 | #define Weekdays @[@"日", @"一", @"二", @"三", @"四", @"五", @"六"] 13 | 14 | static NSDateFormatter *dateFormattor; 15 | 16 | @interface FDCalendar () 17 | 18 | @property (strong, nonatomic) NSDate *date; 19 | 20 | @property (strong, nonatomic) UIButton *titleButton; 21 | @property (strong, nonatomic) UIScrollView *scrollView; 22 | @property (strong, nonatomic) FDCalendarItem *leftCalendarItem; 23 | @property (strong, nonatomic) FDCalendarItem *centerCalendarItem; 24 | @property (strong, nonatomic) FDCalendarItem *rightCalendarItem; 25 | @property (strong, nonatomic) UIView *backgroundView; 26 | @property (strong, nonatomic) UIView *datePickerView; 27 | @property (strong, nonatomic) UIDatePicker *datePicker; 28 | 29 | @end 30 | 31 | @implementation FDCalendar 32 | 33 | - (instancetype)initWithCurrentDate:(NSDate *)date { 34 | if (self = [super init]) { 35 | self.backgroundColor = [UIColor colorWithRed:236 / 255.0 green:236 / 255.0 blue:236 / 255.0 alpha:1.0]; 36 | self.date = date; 37 | 38 | [self setupTitleBar]; 39 | [self setupWeekHeader]; 40 | [self setupCalendarItems]; 41 | [self setupScrollView]; 42 | [self setFrame:CGRectMake(0, 0, DeviceWidth, CGRectGetMaxY(self.scrollView.frame))]; 43 | 44 | [self setCurrentDate:self.date]; 45 | } 46 | return self; 47 | } 48 | 49 | #pragma mark - Custom Accessors 50 | 51 | - (UIView *)backgroundView { 52 | if (!_backgroundView) { 53 | _backgroundView = [[UIView alloc] initWithFrame: self.bounds]; 54 | _backgroundView.backgroundColor = [UIColor blackColor]; 55 | _backgroundView.alpha = 0; 56 | 57 | UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideDatePickerView)]; 58 | [_backgroundView addGestureRecognizer:tapGesture]; 59 | } 60 | 61 | [self addSubview:_backgroundView]; 62 | 63 | return _backgroundView; 64 | } 65 | 66 | - (UIView *)datePickerView { 67 | if (!_datePickerView) { 68 | _datePickerView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, self.frame.size.width, 0)]; 69 | _datePickerView.backgroundColor = [UIColor whiteColor]; 70 | _datePickerView.clipsToBounds = YES; 71 | 72 | UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 10, 32, 20)]; 73 | cancelButton.titleLabel.font = [UIFont boldSystemFontOfSize:16]; 74 | [cancelButton setTitle:@"取消" forState:UIControlStateNormal]; 75 | [cancelButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; 76 | [cancelButton addTarget:self action:@selector(cancelSelectCurrentDate) forControlEvents:UIControlEventTouchUpInside]; 77 | [_datePickerView addSubview:cancelButton]; 78 | 79 | UIButton *okButton = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width - 52, 10, 32, 20)]; 80 | okButton.titleLabel.font = [UIFont boldSystemFontOfSize:16]; 81 | [okButton setTitle:@"确定" forState:UIControlStateNormal]; 82 | [okButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; 83 | [okButton addTarget:self action:@selector(selectCurrentDate) forControlEvents:UIControlEventTouchUpInside]; 84 | [_datePickerView addSubview:okButton]; 85 | 86 | [_datePickerView addSubview:self.datePicker]; 87 | } 88 | 89 | [self addSubview:_datePickerView]; 90 | 91 | return _datePickerView; 92 | } 93 | 94 | - (UIDatePicker *)datePicker { 95 | if (!_datePicker) { 96 | _datePicker = [[UIDatePicker alloc] init]; 97 | _datePicker.datePickerMode = UIDatePickerModeDate; 98 | _datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"Chinese"]; 99 | CGRect frame = _datePicker.frame; 100 | frame.origin = CGPointMake(0, 32); 101 | _datePicker.frame = frame; 102 | } 103 | 104 | return _datePicker; 105 | } 106 | 107 | #pragma mark - Private 108 | 109 | - (NSString *)stringFromDate:(NSDate *)date { 110 | if (!dateFormattor) { 111 | dateFormattor = [[NSDateFormatter alloc] init]; 112 | [dateFormattor setDateFormat:@"MM-yyyy"]; 113 | } 114 | return [dateFormattor stringFromDate:date]; 115 | } 116 | 117 | // 设置上层的titleBar 118 | - (void)setupTitleBar { 119 | UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DeviceWidth, 44)]; 120 | titleView.backgroundColor = [UIColor redColor]; 121 | [self addSubview:titleView]; 122 | 123 | UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 10, 32, 24)]; 124 | [leftButton setImage:[UIImage imageNamed:@"icon_previous"] forState:UIControlStateNormal]; 125 | [leftButton addTarget:self action:@selector(setPreviousMonthDate) forControlEvents:UIControlEventTouchUpInside]; 126 | [titleView addSubview:leftButton]; 127 | 128 | UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(titleView.frame.size.width - 37, 10, 32, 24)]; 129 | [rightButton setImage:[UIImage imageNamed:@"icon_next"] forState:UIControlStateNormal]; 130 | [rightButton addTarget:self action:@selector(setNextMonthDate) forControlEvents:UIControlEventTouchUpInside]; 131 | [titleView addSubview:rightButton]; 132 | 133 | UIButton *titleButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 44)]; 134 | titleButton.titleLabel.textColor = [UIColor whiteColor]; 135 | titleButton.titleLabel.font = [UIFont boldSystemFontOfSize:20]; 136 | titleButton.center = titleView.center; 137 | [titleButton addTarget:self action:@selector(showDatePicker) forControlEvents:UIControlEventTouchUpInside]; 138 | [titleView addSubview:titleButton]; 139 | 140 | self.titleButton = titleButton; 141 | } 142 | 143 | // 设置星期文字的显示 144 | - (void)setupWeekHeader { 145 | NSInteger count = [Weekdays count]; 146 | CGFloat offsetX = 5; 147 | for (int i = 0; i < count; i++) { 148 | UILabel *weekdayLabel = [[UILabel alloc] initWithFrame:CGRectMake(offsetX, 50, (DeviceWidth - 10) / count, 20)]; 149 | weekdayLabel.textAlignment = NSTextAlignmentCenter; 150 | weekdayLabel.text = Weekdays[i]; 151 | 152 | if (i == 0 || i == count - 1) { 153 | weekdayLabel.textColor = [UIColor redColor]; 154 | } else { 155 | weekdayLabel.textColor = [UIColor grayColor]; 156 | } 157 | 158 | [self addSubview:weekdayLabel]; 159 | offsetX += weekdayLabel.frame.size.width; 160 | } 161 | UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(15, 74, DeviceWidth - 30, 1)]; 162 | lineView.backgroundColor = [UIColor lightGrayColor]; 163 | [self addSubview:lineView]; 164 | } 165 | 166 | // 设置包含日历的item的scrollView 167 | - (void)setupScrollView { 168 | self.scrollView.delegate = self; 169 | self.scrollView.pagingEnabled = YES; 170 | self.scrollView.showsHorizontalScrollIndicator = NO; 171 | self.scrollView.showsVerticalScrollIndicator = NO; 172 | [self.scrollView setFrame:CGRectMake(0, 75, DeviceWidth, self.centerCalendarItem.frame.size.height)]; 173 | self.scrollView.contentSize = CGSizeMake(3 * self.scrollView.frame.size.width, self.scrollView.frame.size.height); 174 | self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width, 0); 175 | [self addSubview:self.scrollView]; 176 | } 177 | 178 | // 设置3个日历的item 179 | - (void)setupCalendarItems { 180 | self.scrollView = [[UIScrollView alloc] init]; 181 | 182 | self.leftCalendarItem = [[FDCalendarItem alloc] init]; 183 | [self.scrollView addSubview:self.leftCalendarItem]; 184 | 185 | CGRect itemFrame = self.leftCalendarItem.frame; 186 | itemFrame.origin.x = DeviceWidth; 187 | self.centerCalendarItem = [[FDCalendarItem alloc] init]; 188 | self.centerCalendarItem.frame = itemFrame; 189 | self.centerCalendarItem.delegate = self; 190 | [self.scrollView addSubview:self.centerCalendarItem]; 191 | 192 | itemFrame.origin.x = DeviceWidth * 2; 193 | self.rightCalendarItem = [[FDCalendarItem alloc] init]; 194 | self.rightCalendarItem.frame = itemFrame; 195 | [self.scrollView addSubview:self.rightCalendarItem]; 196 | } 197 | 198 | // 设置当前日期,初始化 199 | - (void)setCurrentDate:(NSDate *)date { 200 | self.centerCalendarItem.date = date; 201 | self.leftCalendarItem.date = [self.centerCalendarItem previousMonthDate]; 202 | self.rightCalendarItem.date = [self.centerCalendarItem nextMonthDate]; 203 | 204 | [self.titleButton setTitle:[self stringFromDate:self.centerCalendarItem.date] forState:UIControlStateNormal]; 205 | } 206 | 207 | // 重新加载日历items的数据 208 | - (void)reloadCalendarItems { 209 | CGPoint offset = self.scrollView.contentOffset; 210 | 211 | if (offset.x == self.scrollView.frame.size.width) { //防止滑动一点点并不切换scrollview的视图 212 | return; 213 | } 214 | 215 | if (offset.x > self.scrollView.frame.size.width) { 216 | [self setNextMonthDate]; 217 | } else { 218 | [self setPreviousMonthDate]; 219 | } 220 | 221 | self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width, 0); 222 | } 223 | 224 | - (void)showDatePickerView { 225 | [UIView animateWithDuration:0.25 animations:^{ 226 | self.backgroundView.alpha = 0.4; 227 | self.datePickerView.frame = CGRectMake(0, 44, self.frame.size.width, 250); 228 | }]; 229 | } 230 | 231 | - (void)hideDatePickerView { 232 | [UIView animateWithDuration:0.25 animations:^{ 233 | self.backgroundView.alpha = 0; 234 | self.datePickerView.frame = CGRectMake(0, 44, self.frame.size.width, 0); 235 | } completion:^(BOOL finished) { 236 | [self.backgroundView removeFromSuperview]; 237 | [self.datePickerView removeFromSuperview]; 238 | }]; 239 | } 240 | 241 | #pragma mark - SEL 242 | 243 | // 跳到上一个月 244 | - (void)setPreviousMonthDate { 245 | [self setCurrentDate:[self.centerCalendarItem previousMonthDate]]; 246 | } 247 | 248 | // 跳到下一个月 249 | - (void)setNextMonthDate { 250 | [self setCurrentDate:[self.centerCalendarItem nextMonthDate]]; 251 | } 252 | 253 | - (void)showDatePicker { 254 | [self showDatePickerView]; 255 | } 256 | 257 | // 选择当前日期 258 | - (void)selectCurrentDate { 259 | [self setCurrentDate:self.datePicker.date]; 260 | [self hideDatePickerView]; 261 | } 262 | 263 | - (void)cancelSelectCurrentDate { 264 | [self hideDatePickerView]; 265 | } 266 | 267 | #pragma mark - UIScrollViewDelegate 268 | 269 | - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { 270 | [self reloadCalendarItems]; 271 | } 272 | 273 | #pragma mark - FDCalendarItemDelegate 274 | 275 | - (void)calendarItem:(FDCalendarItem *)item didSelectedDate:(NSDate *)date { 276 | self.date = date; 277 | [self setCurrentDate:self.date]; 278 | } 279 | 280 | @end 281 | -------------------------------------------------------------------------------- /FDCalendar/FDCalendarItem.h: -------------------------------------------------------------------------------- 1 | // 2 | // FDCalendarItem.h 3 | // FDCalendarDemo 4 | // 5 | // Created by fergusding on 15/8/20. 6 | // Copyright (c) 2015年 fergusding. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | #define DeviceWidth [UIScreen mainScreen].bounds.size.width 12 | 13 | @protocol FDCalendarItemDelegate; 14 | 15 | @interface FDCalendarItem : UIView 16 | 17 | @property (strong, nonatomic) NSDate *date; 18 | @property (weak, nonatomic) id delegate; 19 | 20 | - (NSDate *)nextMonthDate; 21 | - (NSDate *)previousMonthDate; 22 | 23 | @end 24 | 25 | @protocol FDCalendarItemDelegate 26 | 27 | - (void)calendarItem:(FDCalendarItem *)item didSelectedDate:(NSDate *)date; 28 | 29 | @end 30 | -------------------------------------------------------------------------------- /FDCalendar/FDCalendarItem.m: -------------------------------------------------------------------------------- 1 | // 2 | // FDCalendarItem.m 3 | // FDCalendarDemo 4 | // 5 | // Created by fergusding on 15/8/20. 6 | // Copyright (c) 2015年 fergusding. All rights reserved. 7 | // 8 | 9 | #import "FDCalendarItem.h" 10 | 11 | @interface FDCalendarCell : UICollectionViewCell 12 | 13 | - (UILabel *)dayLabel; 14 | - (UILabel *)chineseDayLabel; 15 | 16 | @end 17 | 18 | @implementation FDCalendarCell { 19 | UILabel *_dayLabel; 20 | UILabel *_chineseDayLabel; 21 | } 22 | 23 | - (UILabel *)dayLabel { 24 | if (!_dayLabel) { 25 | _dayLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 20, 20)]; 26 | _dayLabel.textAlignment = NSTextAlignmentCenter; 27 | _dayLabel.font = [UIFont systemFontOfSize:15]; 28 | _dayLabel.center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2 - 3); 29 | [self addSubview:_dayLabel]; 30 | } 31 | return _dayLabel; 32 | } 33 | 34 | - (UILabel *)chineseDayLabel { 35 | if (!_chineseDayLabel) { 36 | _chineseDayLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 20, 10)]; 37 | _chineseDayLabel.textAlignment = NSTextAlignmentCenter; 38 | _chineseDayLabel.font = [UIFont boldSystemFontOfSize:9]; 39 | 40 | CGPoint point = _dayLabel.center; 41 | point.y += 15; 42 | _chineseDayLabel.center = point; 43 | [self addSubview:_chineseDayLabel]; 44 | } 45 | return _chineseDayLabel; 46 | } 47 | 48 | @end 49 | 50 | #define CollectionViewHorizonMargin 5 51 | #define CollectionViewVerticalMargin 5 52 | 53 | #define ChineseMonths @[@"正月", @"二月", @"三月", @"四月", @"五月", @"六月", @"七月", @"八月",@"九月", @"十月", @"冬月", @"腊月"] 54 | #define ChineseDays @[@"初一", @"初二", @"初三", @"初四", @"初五", @"初六", @"初七", @"初八", @"初九", @"初十",@"十一", @"十二", @"十三", @"十四", @"十五", @"十六", @"十七", @"十八", @"十九", @"二十", @"廿一", @"廿二", @"廿三", @"廿四", @"廿五", @"廿六", @"廿七", @"廿八", @"廿九", @"三十"] 55 | 56 | typedef NS_ENUM(NSUInteger, FDCalendarMonth) { 57 | FDCalendarMonthPrevious = 0, 58 | FDCalendarMonthCurrent = 1, 59 | FDCalendarMonthNext = 2, 60 | }; 61 | 62 | @interface FDCalendarItem () 63 | 64 | @property (strong, nonatomic) UICollectionView *collectionView; 65 | 66 | @end 67 | 68 | @implementation FDCalendarItem 69 | 70 | - (instancetype)init { 71 | if (self = [super init]) { 72 | self.backgroundColor = [UIColor clearColor]; 73 | [self setupCollectionView]; 74 | [self setFrame:CGRectMake(0, 0, DeviceWidth, self.collectionView.frame.size.height + CollectionViewVerticalMargin * 2)]; 75 | } 76 | return self; 77 | } 78 | 79 | #pragma mark - Custom Accessors 80 | 81 | - (void)setDate:(NSDate *)date { 82 | _date = date; 83 | [self.collectionView reloadData]; 84 | } 85 | 86 | #pragma mark - Public 87 | 88 | // 获取date的下个月日期 89 | - (NSDate *)nextMonthDate { 90 | NSDateComponents *components = [[NSDateComponents alloc] init]; 91 | components.month = 1; 92 | NSDate *nextMonthDate = [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:self.date options:NSCalendarMatchStrictly]; 93 | return nextMonthDate; 94 | } 95 | 96 | // 获取date的上个月日期 97 | - (NSDate *)previousMonthDate { 98 | NSDateComponents *components = [[NSDateComponents alloc] init]; 99 | components.month = -1; 100 | NSDate *previousMonthDate = [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:self.date options:NSCalendarMatchStrictly]; 101 | return previousMonthDate; 102 | } 103 | 104 | #pragma mark - Private 105 | 106 | // collectionView显示日期单元,设置其属性 107 | - (void)setupCollectionView { 108 | CGFloat itemWidth = (DeviceWidth - CollectionViewHorizonMargin * 2) / 7; 109 | CGFloat itemHeight = itemWidth; 110 | 111 | UICollectionViewFlowLayout *flowLayot = [[UICollectionViewFlowLayout alloc] init]; 112 | flowLayot.sectionInset = UIEdgeInsetsZero; 113 | flowLayot.itemSize = CGSizeMake(itemWidth, itemHeight); 114 | flowLayot.minimumLineSpacing = 0; 115 | flowLayot.minimumInteritemSpacing = 0; 116 | 117 | CGRect collectionViewFrame = CGRectMake(CollectionViewHorizonMargin, CollectionViewVerticalMargin, DeviceWidth - CollectionViewHorizonMargin * 2, itemHeight * 6); 118 | self.collectionView = [[UICollectionView alloc] initWithFrame:collectionViewFrame collectionViewLayout:flowLayot]; 119 | [self addSubview:self.collectionView]; 120 | self.collectionView.dataSource = self; 121 | self.collectionView.delegate = self; 122 | self.collectionView.backgroundColor = [UIColor clearColor]; 123 | [self.collectionView registerClass:[FDCalendarCell class] forCellWithReuseIdentifier:@"CalendarCell"]; 124 | } 125 | 126 | // 获取date当前月的第一天是星期几 127 | - (NSInteger)weekdayOfFirstDayInDate { 128 | NSCalendar *calendar = [NSCalendar currentCalendar]; 129 | [calendar setFirstWeekday:1]; 130 | NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:self.date]; 131 | [components setDay:1]; 132 | NSDate *firstDate = [calendar dateFromComponents:components]; 133 | NSDateComponents *firstComponents = [calendar components:NSCalendarUnitWeekday fromDate:firstDate]; 134 | return firstComponents.weekday - 1; 135 | } 136 | 137 | // 获取date当前月的总天数 138 | - (NSInteger)totalDaysInMonthOfDate:(NSDate *)date { 139 | NSRange range = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date]; 140 | return range.length; 141 | } 142 | 143 | // 获取某月day的日期 144 | - (NSDate *)dateOfMonth:(FDCalendarMonth)calendarMonth WithDay:(NSInteger)day { 145 | NSCalendar *calendar = [NSCalendar currentCalendar]; 146 | NSDate *date; 147 | 148 | switch (calendarMonth) { 149 | case FDCalendarMonthPrevious: 150 | date = [self previousMonthDate]; 151 | break; 152 | 153 | case FDCalendarMonthCurrent: 154 | date = self.date; 155 | break; 156 | 157 | case FDCalendarMonthNext: 158 | date = [self nextMonthDate]; 159 | break; 160 | default: 161 | break; 162 | } 163 | 164 | NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date]; 165 | [components setDay:day]; 166 | NSDate *dateOfDay = [calendar dateFromComponents:components]; 167 | return dateOfDay; 168 | } 169 | 170 | // 获取date当天的农历 171 | - (NSString *)chineseCalendarOfDate:(NSDate *)date { 172 | NSString *day; 173 | NSCalendar *chineseCalendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierChinese]; 174 | NSDateComponents *components = [chineseCalendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date]; 175 | if (components.day == 1) { 176 | day = ChineseMonths[components.month - 1]; 177 | } else { 178 | day = ChineseDays[components.day - 1]; 179 | } 180 | 181 | return day; 182 | } 183 | 184 | #pragma mark - UICollectionDatasource 185 | 186 | - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 187 | return 42; 188 | } 189 | 190 | - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 191 | static NSString *identifier = @"CalendarCell"; 192 | FDCalendarCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 193 | cell.backgroundColor = [UIColor clearColor]; 194 | cell.dayLabel.textColor = [UIColor blackColor]; 195 | cell.chineseDayLabel.textColor = [UIColor grayColor]; 196 | NSInteger firstWeekday = [self weekdayOfFirstDayInDate]; 197 | NSInteger totalDaysOfMonth = [self totalDaysInMonthOfDate:self.date]; 198 | NSInteger totalDaysOfLastMonth = [self totalDaysInMonthOfDate:[self previousMonthDate]]; 199 | 200 | if (indexPath.row < firstWeekday) { // 小于这个月的第一天 201 | NSInteger day = totalDaysOfLastMonth - firstWeekday + indexPath.row + 1; 202 | cell.dayLabel.text = [NSString stringWithFormat:@"%ld", day]; 203 | cell.dayLabel.textColor = [UIColor grayColor]; 204 | cell.chineseDayLabel.text = [self chineseCalendarOfDate:[self dateOfMonth:FDCalendarMonthPrevious WithDay:day]]; 205 | } else if (indexPath.row >= totalDaysOfMonth + firstWeekday) { // 大于这个月的最后一天 206 | NSInteger day = indexPath.row - totalDaysOfMonth - firstWeekday + 1; 207 | cell.dayLabel.text = [NSString stringWithFormat:@"%ld", day]; 208 | cell.dayLabel.textColor = [UIColor grayColor]; 209 | cell.chineseDayLabel.text = [self chineseCalendarOfDate:[self dateOfMonth:FDCalendarMonthNext WithDay:day]]; 210 | } else { // 属于这个月 211 | NSInteger day = indexPath.row - firstWeekday + 1; 212 | cell.dayLabel.text= [NSString stringWithFormat:@"%ld", day]; 213 | 214 | if (day == [[NSCalendar currentCalendar] component:NSCalendarUnitDay fromDate:self.date]) { 215 | cell.backgroundColor = [UIColor redColor]; 216 | cell.layer.cornerRadius = cell.frame.size.height / 2; 217 | cell.dayLabel.textColor = [UIColor whiteColor]; 218 | cell.chineseDayLabel.textColor = [UIColor whiteColor]; 219 | } 220 | 221 | // 如果日期和当期日期同年同月不同天, 注:第一个判断中的方法是iOS8的新API, 会比较传入单元以及比传入单元大得单元上数据是否相等,亲测同时传入Year和Month结果错误 222 | if ([[NSCalendar currentCalendar] isDate:[NSDate date] equalToDate:self.date toUnitGranularity:NSCalendarUnitMonth] && ![[NSCalendar currentCalendar] isDateInToday:self.date]) { 223 | 224 | // 将当前日期的那天高亮显示 225 | if (day == [[NSCalendar currentCalendar] component:NSCalendarUnitDay fromDate:[NSDate date]]) { 226 | cell.dayLabel.textColor = [UIColor redColor]; 227 | } 228 | } 229 | 230 | cell.chineseDayLabel.text = [self chineseCalendarOfDate:[self dateOfMonth:FDCalendarMonthCurrent WithDay:day]]; 231 | } 232 | 233 | 234 | 235 | return cell; 236 | } 237 | 238 | #pragma mark - UICollectionViewDelegate 239 | 240 | - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { 241 | NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:self.date]; 242 | NSInteger firstWeekday = [self weekdayOfFirstDayInDate]; 243 | [components setDay:indexPath.row - firstWeekday + 1]; 244 | NSDate *selectedDate = [[NSCalendar currentCalendar] dateFromComponents:components]; 245 | if (self.delegate && [self.delegate respondsToSelector:@selector(calendarItem:didSelectedDate:)]) { 246 | [self.delegate calendarItem:self didSelectedDate:selectedDate]; 247 | } 248 | } 249 | 250 | @end 251 | -------------------------------------------------------------------------------- /FDCalendarDemo/FDCalendarDemo.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | EA2CE1241B8C43320030F098 /* icon_next@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = EA2CE1221B8C43320030F098 /* icon_next@2x.png */; }; 11 | EA2CE1251B8C43320030F098 /* icon_previous@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = EA2CE1231B8C43320030F098 /* icon_previous@2x.png */; }; 12 | EAFE63991B85C31800DBD193 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = EAFE63981B85C31800DBD193 /* main.m */; }; 13 | EAFE639C1B85C31800DBD193 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = EAFE639B1B85C31800DBD193 /* AppDelegate.m */; }; 14 | EAFE639F1B85C31800DBD193 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = EAFE639E1B85C31800DBD193 /* ViewController.m */; }; 15 | EAFE63A21B85C31800DBD193 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = EAFE63A01B85C31800DBD193 /* Main.storyboard */; }; 16 | EAFE63A41B85C31800DBD193 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = EAFE63A31B85C31800DBD193 /* Images.xcassets */; }; 17 | EAFE63A71B85C31800DBD193 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = EAFE63A51B85C31800DBD193 /* LaunchScreen.xib */; }; 18 | EAFE63B31B85C31900DBD193 /* FDCalendarDemoTests.m in Sources */ = {isa = PBXBuildFile; fileRef = EAFE63B21B85C31900DBD193 /* FDCalendarDemoTests.m */; }; 19 | EAFE63BF1B85C43D00DBD193 /* FDCalendar.m in Sources */ = {isa = PBXBuildFile; fileRef = EAFE63BE1B85C43D00DBD193 /* FDCalendar.m */; }; 20 | EAFE63C21B85C45600DBD193 /* FDCalendarItem.m in Sources */ = {isa = PBXBuildFile; fileRef = EAFE63C11B85C45600DBD193 /* FDCalendarItem.m */; }; 21 | /* End PBXBuildFile section */ 22 | 23 | /* Begin PBXContainerItemProxy section */ 24 | EAFE63AD1B85C31900DBD193 /* PBXContainerItemProxy */ = { 25 | isa = PBXContainerItemProxy; 26 | containerPortal = EAFE638B1B85C31800DBD193 /* Project object */; 27 | proxyType = 1; 28 | remoteGlobalIDString = EAFE63921B85C31800DBD193; 29 | remoteInfo = FDCalendarDemo; 30 | }; 31 | /* End PBXContainerItemProxy section */ 32 | 33 | /* Begin PBXFileReference section */ 34 | EA2CE1221B8C43320030F098 /* icon_next@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = "icon_next@2x.png"; path = "Resource/icon_next@2x.png"; sourceTree = SOURCE_ROOT; }; 35 | EA2CE1231B8C43320030F098 /* icon_previous@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = "icon_previous@2x.png"; path = "Resource/icon_previous@2x.png"; sourceTree = SOURCE_ROOT; }; 36 | EAFE63931B85C31800DBD193 /* FDCalendarDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = FDCalendarDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 37 | EAFE63971B85C31800DBD193 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 38 | EAFE63981B85C31800DBD193 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 39 | EAFE639A1B85C31800DBD193 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 40 | EAFE639B1B85C31800DBD193 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 41 | EAFE639D1B85C31800DBD193 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 42 | EAFE639E1B85C31800DBD193 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 43 | EAFE63A11B85C31800DBD193 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 44 | EAFE63A31B85C31800DBD193 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = ""; }; 45 | EAFE63A61B85C31800DBD193 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/LaunchScreen.xib; sourceTree = ""; }; 46 | EAFE63AC1B85C31900DBD193 /* FDCalendarDemoTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = FDCalendarDemoTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 47 | EAFE63B11B85C31900DBD193 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 48 | EAFE63B21B85C31900DBD193 /* FDCalendarDemoTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FDCalendarDemoTests.m; sourceTree = ""; }; 49 | EAFE63BD1B85C43D00DBD193 /* FDCalendar.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FDCalendar.h; sourceTree = ""; }; 50 | EAFE63BE1B85C43D00DBD193 /* FDCalendar.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FDCalendar.m; sourceTree = ""; }; 51 | EAFE63C01B85C45600DBD193 /* FDCalendarItem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FDCalendarItem.h; sourceTree = ""; }; 52 | EAFE63C11B85C45600DBD193 /* FDCalendarItem.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FDCalendarItem.m; sourceTree = ""; }; 53 | /* End PBXFileReference section */ 54 | 55 | /* Begin PBXFrameworksBuildPhase section */ 56 | EAFE63901B85C31800DBD193 /* Frameworks */ = { 57 | isa = PBXFrameworksBuildPhase; 58 | buildActionMask = 2147483647; 59 | files = ( 60 | ); 61 | runOnlyForDeploymentPostprocessing = 0; 62 | }; 63 | EAFE63A91B85C31900DBD193 /* Frameworks */ = { 64 | isa = PBXFrameworksBuildPhase; 65 | buildActionMask = 2147483647; 66 | files = ( 67 | ); 68 | runOnlyForDeploymentPostprocessing = 0; 69 | }; 70 | /* End PBXFrameworksBuildPhase section */ 71 | 72 | /* Begin PBXGroup section */ 73 | EA2CE1211B8C42BF0030F098 /* Resource */ = { 74 | isa = PBXGroup; 75 | children = ( 76 | EA2CE1221B8C43320030F098 /* icon_next@2x.png */, 77 | EA2CE1231B8C43320030F098 /* icon_previous@2x.png */, 78 | ); 79 | name = Resource; 80 | sourceTree = ""; 81 | }; 82 | EAFE638A1B85C31800DBD193 = { 83 | isa = PBXGroup; 84 | children = ( 85 | EAFE63951B85C31800DBD193 /* FDCalendarDemo */, 86 | EAFE63AF1B85C31900DBD193 /* FDCalendarDemoTests */, 87 | EAFE63941B85C31800DBD193 /* Products */, 88 | ); 89 | sourceTree = ""; 90 | }; 91 | EAFE63941B85C31800DBD193 /* Products */ = { 92 | isa = PBXGroup; 93 | children = ( 94 | EAFE63931B85C31800DBD193 /* FDCalendarDemo.app */, 95 | EAFE63AC1B85C31900DBD193 /* FDCalendarDemoTests.xctest */, 96 | ); 97 | name = Products; 98 | sourceTree = ""; 99 | }; 100 | EAFE63951B85C31800DBD193 /* FDCalendarDemo */ = { 101 | isa = PBXGroup; 102 | children = ( 103 | EAFE63BC1B85C40C00DBD193 /* FDCalendar */, 104 | EAFE639A1B85C31800DBD193 /* AppDelegate.h */, 105 | EAFE639B1B85C31800DBD193 /* AppDelegate.m */, 106 | EAFE639D1B85C31800DBD193 /* ViewController.h */, 107 | EAFE639E1B85C31800DBD193 /* ViewController.m */, 108 | EAFE63A01B85C31800DBD193 /* Main.storyboard */, 109 | EAFE63A31B85C31800DBD193 /* Images.xcassets */, 110 | EAFE63A51B85C31800DBD193 /* LaunchScreen.xib */, 111 | EAFE63961B85C31800DBD193 /* Supporting Files */, 112 | ); 113 | path = FDCalendarDemo; 114 | sourceTree = ""; 115 | }; 116 | EAFE63961B85C31800DBD193 /* Supporting Files */ = { 117 | isa = PBXGroup; 118 | children = ( 119 | EAFE63971B85C31800DBD193 /* Info.plist */, 120 | EAFE63981B85C31800DBD193 /* main.m */, 121 | ); 122 | name = "Supporting Files"; 123 | sourceTree = ""; 124 | }; 125 | EAFE63AF1B85C31900DBD193 /* FDCalendarDemoTests */ = { 126 | isa = PBXGroup; 127 | children = ( 128 | EAFE63B21B85C31900DBD193 /* FDCalendarDemoTests.m */, 129 | EAFE63B01B85C31900DBD193 /* Supporting Files */, 130 | ); 131 | path = FDCalendarDemoTests; 132 | sourceTree = ""; 133 | }; 134 | EAFE63B01B85C31900DBD193 /* Supporting Files */ = { 135 | isa = PBXGroup; 136 | children = ( 137 | EAFE63B11B85C31900DBD193 /* Info.plist */, 138 | ); 139 | name = "Supporting Files"; 140 | sourceTree = ""; 141 | }; 142 | EAFE63BC1B85C40C00DBD193 /* FDCalendar */ = { 143 | isa = PBXGroup; 144 | children = ( 145 | EA2CE1211B8C42BF0030F098 /* Resource */, 146 | EAFE63BD1B85C43D00DBD193 /* FDCalendar.h */, 147 | EAFE63BE1B85C43D00DBD193 /* FDCalendar.m */, 148 | EAFE63C01B85C45600DBD193 /* FDCalendarItem.h */, 149 | EAFE63C11B85C45600DBD193 /* FDCalendarItem.m */, 150 | ); 151 | name = FDCalendar; 152 | sourceTree = ""; 153 | }; 154 | /* End PBXGroup section */ 155 | 156 | /* Begin PBXNativeTarget section */ 157 | EAFE63921B85C31800DBD193 /* FDCalendarDemo */ = { 158 | isa = PBXNativeTarget; 159 | buildConfigurationList = EAFE63B61B85C31900DBD193 /* Build configuration list for PBXNativeTarget "FDCalendarDemo" */; 160 | buildPhases = ( 161 | EAFE638F1B85C31800DBD193 /* Sources */, 162 | EAFE63901B85C31800DBD193 /* Frameworks */, 163 | EAFE63911B85C31800DBD193 /* Resources */, 164 | ); 165 | buildRules = ( 166 | ); 167 | dependencies = ( 168 | ); 169 | name = FDCalendarDemo; 170 | productName = FDCalendarDemo; 171 | productReference = EAFE63931B85C31800DBD193 /* FDCalendarDemo.app */; 172 | productType = "com.apple.product-type.application"; 173 | }; 174 | EAFE63AB1B85C31900DBD193 /* FDCalendarDemoTests */ = { 175 | isa = PBXNativeTarget; 176 | buildConfigurationList = EAFE63B91B85C31900DBD193 /* Build configuration list for PBXNativeTarget "FDCalendarDemoTests" */; 177 | buildPhases = ( 178 | EAFE63A81B85C31900DBD193 /* Sources */, 179 | EAFE63A91B85C31900DBD193 /* Frameworks */, 180 | EAFE63AA1B85C31900DBD193 /* Resources */, 181 | ); 182 | buildRules = ( 183 | ); 184 | dependencies = ( 185 | EAFE63AE1B85C31900DBD193 /* PBXTargetDependency */, 186 | ); 187 | name = FDCalendarDemoTests; 188 | productName = FDCalendarDemoTests; 189 | productReference = EAFE63AC1B85C31900DBD193 /* FDCalendarDemoTests.xctest */; 190 | productType = "com.apple.product-type.bundle.unit-test"; 191 | }; 192 | /* End PBXNativeTarget section */ 193 | 194 | /* Begin PBXProject section */ 195 | EAFE638B1B85C31800DBD193 /* Project object */ = { 196 | isa = PBXProject; 197 | attributes = { 198 | LastUpgradeCheck = 0630; 199 | ORGANIZATIONNAME = fergusding; 200 | TargetAttributes = { 201 | EAFE63921B85C31800DBD193 = { 202 | CreatedOnToolsVersion = 6.3.1; 203 | }; 204 | EAFE63AB1B85C31900DBD193 = { 205 | CreatedOnToolsVersion = 6.3.1; 206 | TestTargetID = EAFE63921B85C31800DBD193; 207 | }; 208 | }; 209 | }; 210 | buildConfigurationList = EAFE638E1B85C31800DBD193 /* Build configuration list for PBXProject "FDCalendarDemo" */; 211 | compatibilityVersion = "Xcode 3.2"; 212 | developmentRegion = English; 213 | hasScannedForEncodings = 0; 214 | knownRegions = ( 215 | en, 216 | Base, 217 | ); 218 | mainGroup = EAFE638A1B85C31800DBD193; 219 | productRefGroup = EAFE63941B85C31800DBD193 /* Products */; 220 | projectDirPath = ""; 221 | projectRoot = ""; 222 | targets = ( 223 | EAFE63921B85C31800DBD193 /* FDCalendarDemo */, 224 | EAFE63AB1B85C31900DBD193 /* FDCalendarDemoTests */, 225 | ); 226 | }; 227 | /* End PBXProject section */ 228 | 229 | /* Begin PBXResourcesBuildPhase section */ 230 | EAFE63911B85C31800DBD193 /* Resources */ = { 231 | isa = PBXResourcesBuildPhase; 232 | buildActionMask = 2147483647; 233 | files = ( 234 | EAFE63A21B85C31800DBD193 /* Main.storyboard in Resources */, 235 | EAFE63A71B85C31800DBD193 /* LaunchScreen.xib in Resources */, 236 | EA2CE1251B8C43320030F098 /* icon_previous@2x.png in Resources */, 237 | EAFE63A41B85C31800DBD193 /* Images.xcassets in Resources */, 238 | EA2CE1241B8C43320030F098 /* icon_next@2x.png in Resources */, 239 | ); 240 | runOnlyForDeploymentPostprocessing = 0; 241 | }; 242 | EAFE63AA1B85C31900DBD193 /* Resources */ = { 243 | isa = PBXResourcesBuildPhase; 244 | buildActionMask = 2147483647; 245 | files = ( 246 | ); 247 | runOnlyForDeploymentPostprocessing = 0; 248 | }; 249 | /* End PBXResourcesBuildPhase section */ 250 | 251 | /* Begin PBXSourcesBuildPhase section */ 252 | EAFE638F1B85C31800DBD193 /* Sources */ = { 253 | isa = PBXSourcesBuildPhase; 254 | buildActionMask = 2147483647; 255 | files = ( 256 | EAFE639F1B85C31800DBD193 /* ViewController.m in Sources */, 257 | EAFE63C21B85C45600DBD193 /* FDCalendarItem.m in Sources */, 258 | EAFE639C1B85C31800DBD193 /* AppDelegate.m in Sources */, 259 | EAFE63991B85C31800DBD193 /* main.m in Sources */, 260 | EAFE63BF1B85C43D00DBD193 /* FDCalendar.m in Sources */, 261 | ); 262 | runOnlyForDeploymentPostprocessing = 0; 263 | }; 264 | EAFE63A81B85C31900DBD193 /* Sources */ = { 265 | isa = PBXSourcesBuildPhase; 266 | buildActionMask = 2147483647; 267 | files = ( 268 | EAFE63B31B85C31900DBD193 /* FDCalendarDemoTests.m in Sources */, 269 | ); 270 | runOnlyForDeploymentPostprocessing = 0; 271 | }; 272 | /* End PBXSourcesBuildPhase section */ 273 | 274 | /* Begin PBXTargetDependency section */ 275 | EAFE63AE1B85C31900DBD193 /* PBXTargetDependency */ = { 276 | isa = PBXTargetDependency; 277 | target = EAFE63921B85C31800DBD193 /* FDCalendarDemo */; 278 | targetProxy = EAFE63AD1B85C31900DBD193 /* PBXContainerItemProxy */; 279 | }; 280 | /* End PBXTargetDependency section */ 281 | 282 | /* Begin PBXVariantGroup section */ 283 | EAFE63A01B85C31800DBD193 /* Main.storyboard */ = { 284 | isa = PBXVariantGroup; 285 | children = ( 286 | EAFE63A11B85C31800DBD193 /* Base */, 287 | ); 288 | name = Main.storyboard; 289 | sourceTree = ""; 290 | }; 291 | EAFE63A51B85C31800DBD193 /* LaunchScreen.xib */ = { 292 | isa = PBXVariantGroup; 293 | children = ( 294 | EAFE63A61B85C31800DBD193 /* Base */, 295 | ); 296 | name = LaunchScreen.xib; 297 | sourceTree = ""; 298 | }; 299 | /* End PBXVariantGroup section */ 300 | 301 | /* Begin XCBuildConfiguration section */ 302 | EAFE63B41B85C31900DBD193 /* Debug */ = { 303 | isa = XCBuildConfiguration; 304 | buildSettings = { 305 | ALWAYS_SEARCH_USER_PATHS = NO; 306 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 307 | CLANG_CXX_LIBRARY = "libc++"; 308 | CLANG_ENABLE_MODULES = YES; 309 | CLANG_ENABLE_OBJC_ARC = YES; 310 | CLANG_WARN_BOOL_CONVERSION = YES; 311 | CLANG_WARN_CONSTANT_CONVERSION = YES; 312 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 313 | CLANG_WARN_EMPTY_BODY = YES; 314 | CLANG_WARN_ENUM_CONVERSION = YES; 315 | CLANG_WARN_INT_CONVERSION = YES; 316 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 317 | CLANG_WARN_UNREACHABLE_CODE = YES; 318 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 319 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 320 | COPY_PHASE_STRIP = NO; 321 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 322 | ENABLE_STRICT_OBJC_MSGSEND = YES; 323 | GCC_C_LANGUAGE_STANDARD = gnu99; 324 | GCC_DYNAMIC_NO_PIC = NO; 325 | GCC_NO_COMMON_BLOCKS = YES; 326 | GCC_OPTIMIZATION_LEVEL = 0; 327 | GCC_PREPROCESSOR_DEFINITIONS = ( 328 | "DEBUG=1", 329 | "$(inherited)", 330 | ); 331 | GCC_SYMBOLS_PRIVATE_EXTERN = NO; 332 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 333 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 334 | GCC_WARN_UNDECLARED_SELECTOR = YES; 335 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 336 | GCC_WARN_UNUSED_FUNCTION = YES; 337 | GCC_WARN_UNUSED_VARIABLE = YES; 338 | IPHONEOS_DEPLOYMENT_TARGET = 8.3; 339 | MTL_ENABLE_DEBUG_INFO = YES; 340 | ONLY_ACTIVE_ARCH = YES; 341 | SDKROOT = iphoneos; 342 | TARGETED_DEVICE_FAMILY = "1,2"; 343 | }; 344 | name = Debug; 345 | }; 346 | EAFE63B51B85C31900DBD193 /* Release */ = { 347 | isa = XCBuildConfiguration; 348 | buildSettings = { 349 | ALWAYS_SEARCH_USER_PATHS = NO; 350 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 351 | CLANG_CXX_LIBRARY = "libc++"; 352 | CLANG_ENABLE_MODULES = YES; 353 | CLANG_ENABLE_OBJC_ARC = YES; 354 | CLANG_WARN_BOOL_CONVERSION = YES; 355 | CLANG_WARN_CONSTANT_CONVERSION = YES; 356 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 357 | CLANG_WARN_EMPTY_BODY = YES; 358 | CLANG_WARN_ENUM_CONVERSION = YES; 359 | CLANG_WARN_INT_CONVERSION = YES; 360 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 361 | CLANG_WARN_UNREACHABLE_CODE = YES; 362 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 363 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 364 | COPY_PHASE_STRIP = NO; 365 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 366 | ENABLE_NS_ASSERTIONS = NO; 367 | ENABLE_STRICT_OBJC_MSGSEND = YES; 368 | GCC_C_LANGUAGE_STANDARD = gnu99; 369 | GCC_NO_COMMON_BLOCKS = YES; 370 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 371 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 372 | GCC_WARN_UNDECLARED_SELECTOR = YES; 373 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 374 | GCC_WARN_UNUSED_FUNCTION = YES; 375 | GCC_WARN_UNUSED_VARIABLE = YES; 376 | IPHONEOS_DEPLOYMENT_TARGET = 8.3; 377 | MTL_ENABLE_DEBUG_INFO = NO; 378 | SDKROOT = iphoneos; 379 | TARGETED_DEVICE_FAMILY = "1,2"; 380 | VALIDATE_PRODUCT = YES; 381 | }; 382 | name = Release; 383 | }; 384 | EAFE63B71B85C31900DBD193 /* Debug */ = { 385 | isa = XCBuildConfiguration; 386 | buildSettings = { 387 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 388 | INFOPLIST_FILE = FDCalendarDemo/Info.plist; 389 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 390 | PRODUCT_NAME = "$(TARGET_NAME)"; 391 | }; 392 | name = Debug; 393 | }; 394 | EAFE63B81B85C31900DBD193 /* Release */ = { 395 | isa = XCBuildConfiguration; 396 | buildSettings = { 397 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 398 | INFOPLIST_FILE = FDCalendarDemo/Info.plist; 399 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 400 | PRODUCT_NAME = "$(TARGET_NAME)"; 401 | }; 402 | name = Release; 403 | }; 404 | EAFE63BA1B85C31900DBD193 /* Debug */ = { 405 | isa = XCBuildConfiguration; 406 | buildSettings = { 407 | BUNDLE_LOADER = "$(TEST_HOST)"; 408 | FRAMEWORK_SEARCH_PATHS = ( 409 | "$(SDKROOT)/Developer/Library/Frameworks", 410 | "$(inherited)", 411 | ); 412 | GCC_PREPROCESSOR_DEFINITIONS = ( 413 | "DEBUG=1", 414 | "$(inherited)", 415 | ); 416 | INFOPLIST_FILE = FDCalendarDemoTests/Info.plist; 417 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 418 | PRODUCT_NAME = "$(TARGET_NAME)"; 419 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/FDCalendarDemo.app/FDCalendarDemo"; 420 | }; 421 | name = Debug; 422 | }; 423 | EAFE63BB1B85C31900DBD193 /* Release */ = { 424 | isa = XCBuildConfiguration; 425 | buildSettings = { 426 | BUNDLE_LOADER = "$(TEST_HOST)"; 427 | FRAMEWORK_SEARCH_PATHS = ( 428 | "$(SDKROOT)/Developer/Library/Frameworks", 429 | "$(inherited)", 430 | ); 431 | INFOPLIST_FILE = FDCalendarDemoTests/Info.plist; 432 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 433 | PRODUCT_NAME = "$(TARGET_NAME)"; 434 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/FDCalendarDemo.app/FDCalendarDemo"; 435 | }; 436 | name = Release; 437 | }; 438 | /* End XCBuildConfiguration section */ 439 | 440 | /* Begin XCConfigurationList section */ 441 | EAFE638E1B85C31800DBD193 /* Build configuration list for PBXProject "FDCalendarDemo" */ = { 442 | isa = XCConfigurationList; 443 | buildConfigurations = ( 444 | EAFE63B41B85C31900DBD193 /* Debug */, 445 | EAFE63B51B85C31900DBD193 /* Release */, 446 | ); 447 | defaultConfigurationIsVisible = 0; 448 | defaultConfigurationName = Release; 449 | }; 450 | EAFE63B61B85C31900DBD193 /* Build configuration list for PBXNativeTarget "FDCalendarDemo" */ = { 451 | isa = XCConfigurationList; 452 | buildConfigurations = ( 453 | EAFE63B71B85C31900DBD193 /* Debug */, 454 | EAFE63B81B85C31900DBD193 /* Release */, 455 | ); 456 | defaultConfigurationIsVisible = 0; 457 | defaultConfigurationName = Release; 458 | }; 459 | EAFE63B91B85C31900DBD193 /* Build configuration list for PBXNativeTarget "FDCalendarDemoTests" */ = { 460 | isa = XCConfigurationList; 461 | buildConfigurations = ( 462 | EAFE63BA1B85C31900DBD193 /* Debug */, 463 | EAFE63BB1B85C31900DBD193 /* Release */, 464 | ); 465 | defaultConfigurationIsVisible = 0; 466 | defaultConfigurationName = Release; 467 | }; 468 | /* End XCConfigurationList section */ 469 | }; 470 | rootObject = EAFE638B1B85C31800DBD193 /* Project object */; 471 | } 472 | -------------------------------------------------------------------------------- /FDCalendarDemo/FDCalendarDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /FDCalendarDemo/FDCalendarDemo/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // FDCalendarDemo 4 | // 5 | // Created by fergusding on 15/8/20. 6 | // Copyright (c) 2015年 fergusding. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /FDCalendarDemo/FDCalendarDemo/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // FDCalendarDemo 4 | // 5 | // Created by fergusding on 15/8/20. 6 | // Copyright (c) 2015年 fergusding. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | - (void)applicationWillResignActive:(UIApplication *)application { 24 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 25 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 26 | } 27 | 28 | - (void)applicationDidEnterBackground:(UIApplication *)application { 29 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 30 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 31 | } 32 | 33 | - (void)applicationWillEnterForeground:(UIApplication *)application { 34 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 35 | } 36 | 37 | - (void)applicationDidBecomeActive:(UIApplication *)application { 38 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 39 | } 40 | 41 | - (void)applicationWillTerminate:(UIApplication *)application { 42 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 43 | } 44 | 45 | @end 46 | -------------------------------------------------------------------------------- /FDCalendarDemo/FDCalendarDemo/Base.lproj/LaunchScreen.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 20 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /FDCalendarDemo/FDCalendarDemo/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /FDCalendarDemo/FDCalendarDemo/FDCalendar.h: -------------------------------------------------------------------------------- 1 | // 2 | // FDCalendar.h 3 | // FDCalendarDemo 4 | // 5 | // Created by fergusding on 15/8/20. 6 | // Copyright (c) 2015年 fergusding. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface FDCalendar : UIView 12 | 13 | - (instancetype)initWithCurrentDate:(NSDate *)date; 14 | 15 | @end 16 | -------------------------------------------------------------------------------- /FDCalendarDemo/FDCalendarDemo/FDCalendar.m: -------------------------------------------------------------------------------- 1 | // 2 | // FDCalendar.m 3 | // FDCalendarDemo 4 | // 5 | // Created by fergusding on 15/8/20. 6 | // Copyright (c) 2015年 fergusding. All rights reserved. 7 | // 8 | 9 | #import "FDCalendar.h" 10 | #import "FDCalendarItem.h" 11 | 12 | #define Weekdays @[@"日", @"一", @"二", @"三", @"四", @"五", @"六"] 13 | 14 | static NSDateFormatter *dateFormattor; 15 | 16 | @interface FDCalendar () 17 | 18 | @property (strong, nonatomic) NSDate *date; 19 | 20 | @property (strong, nonatomic) UIButton *titleButton; 21 | @property (strong, nonatomic) UIScrollView *scrollView; 22 | @property (strong, nonatomic) FDCalendarItem *leftCalendarItem; 23 | @property (strong, nonatomic) FDCalendarItem *centerCalendarItem; 24 | @property (strong, nonatomic) FDCalendarItem *rightCalendarItem; 25 | @property (strong, nonatomic) UIView *backgroundView; 26 | @property (strong, nonatomic) UIView *datePickerView; 27 | @property (strong, nonatomic) UIDatePicker *datePicker; 28 | 29 | @end 30 | 31 | @implementation FDCalendar 32 | 33 | - (instancetype)initWithCurrentDate:(NSDate *)date { 34 | if (self = [super init]) { 35 | self.backgroundColor = [UIColor colorWithRed:236 / 255.0 green:236 / 255.0 blue:236 / 255.0 alpha:1.0]; 36 | self.date = date; 37 | 38 | [self setupTitleBar]; 39 | [self setupWeekHeader]; 40 | [self setupCalendarItems]; 41 | [self setupScrollView]; 42 | [self setFrame:CGRectMake(0, 0, DeviceWidth, CGRectGetMaxY(self.scrollView.frame))]; 43 | 44 | [self setCurrentDate:self.date]; 45 | } 46 | return self; 47 | } 48 | 49 | #pragma mark - Custom Accessors 50 | 51 | - (UIView *)backgroundView { 52 | if (!_backgroundView) { 53 | _backgroundView = [[UIView alloc] initWithFrame: self.bounds]; 54 | _backgroundView.backgroundColor = [UIColor blackColor]; 55 | _backgroundView.alpha = 0; 56 | 57 | UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideDatePickerView)]; 58 | [_backgroundView addGestureRecognizer:tapGesture]; 59 | } 60 | 61 | [self addSubview:_backgroundView]; 62 | 63 | return _backgroundView; 64 | } 65 | 66 | - (UIView *)datePickerView { 67 | if (!_datePickerView) { 68 | _datePickerView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, self.frame.size.width, 0)]; 69 | _datePickerView.backgroundColor = [UIColor whiteColor]; 70 | _datePickerView.clipsToBounds = YES; 71 | 72 | UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 10, 32, 20)]; 73 | cancelButton.titleLabel.font = [UIFont boldSystemFontOfSize:16]; 74 | [cancelButton setTitle:@"取消" forState:UIControlStateNormal]; 75 | [cancelButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; 76 | [cancelButton addTarget:self action:@selector(cancelSelectCurrentDate) forControlEvents:UIControlEventTouchUpInside]; 77 | [_datePickerView addSubview:cancelButton]; 78 | 79 | UIButton *okButton = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width - 52, 10, 32, 20)]; 80 | okButton.titleLabel.font = [UIFont boldSystemFontOfSize:16]; 81 | [okButton setTitle:@"确定" forState:UIControlStateNormal]; 82 | [okButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; 83 | [okButton addTarget:self action:@selector(selectCurrentDate) forControlEvents:UIControlEventTouchUpInside]; 84 | [_datePickerView addSubview:okButton]; 85 | 86 | [_datePickerView addSubview:self.datePicker]; 87 | } 88 | 89 | [self addSubview:_datePickerView]; 90 | 91 | return _datePickerView; 92 | } 93 | 94 | - (UIDatePicker *)datePicker { 95 | if (!_datePicker) { 96 | _datePicker = [[UIDatePicker alloc] init]; 97 | _datePicker.datePickerMode = UIDatePickerModeDate; 98 | _datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"Chinese"]; 99 | CGRect frame = _datePicker.frame; 100 | frame.origin = CGPointMake(0, 32); 101 | _datePicker.frame = frame; 102 | } 103 | 104 | return _datePicker; 105 | } 106 | 107 | #pragma mark - Private 108 | 109 | - (NSString *)stringFromDate:(NSDate *)date { 110 | if (!dateFormattor) { 111 | dateFormattor = [[NSDateFormatter alloc] init]; 112 | [dateFormattor setDateFormat:@"MM-yyyy"]; 113 | } 114 | return [dateFormattor stringFromDate:date]; 115 | } 116 | 117 | // 设置上层的titleBar 118 | - (void)setupTitleBar { 119 | UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DeviceWidth, 44)]; 120 | titleView.backgroundColor = [UIColor redColor]; 121 | [self addSubview:titleView]; 122 | 123 | UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 10, 32, 24)]; 124 | [leftButton setImage:[UIImage imageNamed:@"icon_previous"] forState:UIControlStateNormal]; 125 | [leftButton addTarget:self action:@selector(setPreviousMonthDate) forControlEvents:UIControlEventTouchUpInside]; 126 | [titleView addSubview:leftButton]; 127 | 128 | UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(titleView.frame.size.width - 37, 10, 32, 24)]; 129 | [rightButton setImage:[UIImage imageNamed:@"icon_next"] forState:UIControlStateNormal]; 130 | [rightButton addTarget:self action:@selector(setNextMonthDate) forControlEvents:UIControlEventTouchUpInside]; 131 | [titleView addSubview:rightButton]; 132 | 133 | UIButton *titleButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 44)]; 134 | titleButton.titleLabel.textColor = [UIColor whiteColor]; 135 | titleButton.titleLabel.font = [UIFont boldSystemFontOfSize:20]; 136 | titleButton.center = titleView.center; 137 | [titleButton addTarget:self action:@selector(showDatePicker) forControlEvents:UIControlEventTouchUpInside]; 138 | [titleView addSubview:titleButton]; 139 | 140 | self.titleButton = titleButton; 141 | } 142 | 143 | // 设置星期文字的显示 144 | - (void)setupWeekHeader { 145 | NSInteger count = [Weekdays count]; 146 | CGFloat offsetX = 5; 147 | for (int i = 0; i < count; i++) { 148 | UILabel *weekdayLabel = [[UILabel alloc] initWithFrame:CGRectMake(offsetX, 50, (DeviceWidth - 10) / count, 20)]; 149 | weekdayLabel.textAlignment = NSTextAlignmentCenter; 150 | weekdayLabel.text = Weekdays[i]; 151 | 152 | if (i == 0 || i == count - 1) { 153 | weekdayLabel.textColor = [UIColor redColor]; 154 | } else { 155 | weekdayLabel.textColor = [UIColor grayColor]; 156 | } 157 | 158 | [self addSubview:weekdayLabel]; 159 | offsetX += weekdayLabel.frame.size.width; 160 | } 161 | UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(15, 74, DeviceWidth - 30, 1)]; 162 | lineView.backgroundColor = [UIColor lightGrayColor]; 163 | [self addSubview:lineView]; 164 | } 165 | 166 | // 设置包含日历的item的scrollView 167 | - (void)setupScrollView { 168 | self.scrollView.delegate = self; 169 | self.scrollView.pagingEnabled = YES; 170 | self.scrollView.showsHorizontalScrollIndicator = NO; 171 | self.scrollView.showsVerticalScrollIndicator = NO; 172 | [self.scrollView setFrame:CGRectMake(0, 75, DeviceWidth, self.centerCalendarItem.frame.size.height)]; 173 | self.scrollView.contentSize = CGSizeMake(3 * self.scrollView.frame.size.width, self.scrollView.frame.size.height); 174 | self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width, 0); 175 | [self addSubview:self.scrollView]; 176 | } 177 | 178 | // 设置3个日历的item 179 | - (void)setupCalendarItems { 180 | self.scrollView = [[UIScrollView alloc] init]; 181 | 182 | self.leftCalendarItem = [[FDCalendarItem alloc] init]; 183 | [self.scrollView addSubview:self.leftCalendarItem]; 184 | 185 | CGRect itemFrame = self.leftCalendarItem.frame; 186 | itemFrame.origin.x = DeviceWidth; 187 | self.centerCalendarItem = [[FDCalendarItem alloc] init]; 188 | self.centerCalendarItem.frame = itemFrame; 189 | self.centerCalendarItem.delegate = self; 190 | [self.scrollView addSubview:self.centerCalendarItem]; 191 | 192 | itemFrame.origin.x = DeviceWidth * 2; 193 | self.rightCalendarItem = [[FDCalendarItem alloc] init]; 194 | self.rightCalendarItem.frame = itemFrame; 195 | [self.scrollView addSubview:self.rightCalendarItem]; 196 | } 197 | 198 | // 设置当前日期,初始化 199 | - (void)setCurrentDate:(NSDate *)date { 200 | self.centerCalendarItem.date = date; 201 | self.leftCalendarItem.date = [self.centerCalendarItem previousMonthDate]; 202 | self.rightCalendarItem.date = [self.centerCalendarItem nextMonthDate]; 203 | 204 | [self.titleButton setTitle:[self stringFromDate:self.centerCalendarItem.date] forState:UIControlStateNormal]; 205 | } 206 | 207 | // 重新加载日历items的数据 208 | - (void)reloadCalendarItems { 209 | CGPoint offset = self.scrollView.contentOffset; 210 | 211 | if (offset.x == self.scrollView.frame.size.width) { //防止滑动一点点并不切换scrollview的视图 212 | return; 213 | } 214 | 215 | if (offset.x > self.scrollView.frame.size.width) { 216 | [self setNextMonthDate]; 217 | } else { 218 | [self setPreviousMonthDate]; 219 | } 220 | 221 | self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width, 0); 222 | } 223 | 224 | - (void)showDatePickerView { 225 | [UIView animateWithDuration:0.25 animations:^{ 226 | self.backgroundView.alpha = 0.4; 227 | self.datePickerView.frame = CGRectMake(0, 44, self.frame.size.width, 250); 228 | }]; 229 | } 230 | 231 | - (void)hideDatePickerView { 232 | [UIView animateWithDuration:0.25 animations:^{ 233 | self.backgroundView.alpha = 0; 234 | self.datePickerView.frame = CGRectMake(0, 44, self.frame.size.width, 0); 235 | } completion:^(BOOL finished) { 236 | [self.backgroundView removeFromSuperview]; 237 | [self.datePickerView removeFromSuperview]; 238 | }]; 239 | } 240 | 241 | #pragma mark - SEL 242 | 243 | // 跳到上一个月 244 | - (void)setPreviousMonthDate { 245 | [self setCurrentDate:[self.centerCalendarItem previousMonthDate]]; 246 | } 247 | 248 | // 跳到下一个月 249 | - (void)setNextMonthDate { 250 | [self setCurrentDate:[self.centerCalendarItem nextMonthDate]]; 251 | } 252 | 253 | - (void)showDatePicker { 254 | [self showDatePickerView]; 255 | } 256 | 257 | // 选择当前日期 258 | - (void)selectCurrentDate { 259 | [self setCurrentDate:self.datePicker.date]; 260 | [self hideDatePickerView]; 261 | } 262 | 263 | - (void)cancelSelectCurrentDate { 264 | [self hideDatePickerView]; 265 | } 266 | 267 | #pragma mark - UIScrollViewDelegate 268 | 269 | - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { 270 | [self reloadCalendarItems]; 271 | } 272 | 273 | #pragma mark - FDCalendarItemDelegate 274 | 275 | - (void)calendarItem:(FDCalendarItem *)item didSelectedDate:(NSDate *)date { 276 | self.date = date; 277 | [self setCurrentDate:self.date]; 278 | } 279 | 280 | @end 281 | -------------------------------------------------------------------------------- /FDCalendarDemo/FDCalendarDemo/FDCalendarItem.h: -------------------------------------------------------------------------------- 1 | // 2 | // FDCalendarItem.h 3 | // FDCalendarDemo 4 | // 5 | // Created by fergusding on 15/8/20. 6 | // Copyright (c) 2015年 fergusding. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | #define DeviceWidth [UIScreen mainScreen].bounds.size.width 12 | 13 | @protocol FDCalendarItemDelegate; 14 | 15 | @interface FDCalendarItem : UIView 16 | 17 | @property (strong, nonatomic) NSDate *date; 18 | @property (weak, nonatomic) id delegate; 19 | 20 | - (NSDate *)nextMonthDate; 21 | - (NSDate *)previousMonthDate; 22 | 23 | @end 24 | 25 | @protocol FDCalendarItemDelegate 26 | 27 | - (void)calendarItem:(FDCalendarItem *)item didSelectedDate:(NSDate *)date; 28 | 29 | @end 30 | -------------------------------------------------------------------------------- /FDCalendarDemo/FDCalendarDemo/FDCalendarItem.m: -------------------------------------------------------------------------------- 1 | // 2 | // FDCalendarItem.m 3 | // FDCalendarDemo 4 | // 5 | // Created by fergusding on 15/8/20. 6 | // Copyright (c) 2015年 fergusding. All rights reserved. 7 | // 8 | 9 | #import "FDCalendarItem.h" 10 | 11 | @interface FDCalendarCell : UICollectionViewCell 12 | 13 | - (UILabel *)dayLabel; 14 | - (UILabel *)chineseDayLabel; 15 | 16 | @end 17 | 18 | @implementation FDCalendarCell { 19 | UILabel *_dayLabel; 20 | UILabel *_chineseDayLabel; 21 | } 22 | 23 | - (UILabel *)dayLabel { 24 | if (!_dayLabel) { 25 | _dayLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 20, 20)]; 26 | _dayLabel.textAlignment = NSTextAlignmentCenter; 27 | _dayLabel.font = [UIFont systemFontOfSize:15]; 28 | _dayLabel.center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2 - 3); 29 | [self addSubview:_dayLabel]; 30 | } 31 | return _dayLabel; 32 | } 33 | 34 | - (UILabel *)chineseDayLabel { 35 | if (!_chineseDayLabel) { 36 | _chineseDayLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 20, 10)]; 37 | _chineseDayLabel.textAlignment = NSTextAlignmentCenter; 38 | _chineseDayLabel.font = [UIFont boldSystemFontOfSize:9]; 39 | 40 | CGPoint point = _dayLabel.center; 41 | point.y += 15; 42 | _chineseDayLabel.center = point; 43 | [self addSubview:_chineseDayLabel]; 44 | } 45 | return _chineseDayLabel; 46 | } 47 | 48 | @end 49 | 50 | #define CollectionViewHorizonMargin 5 51 | #define CollectionViewVerticalMargin 5 52 | 53 | #define ChineseMonths @[@"正月", @"二月", @"三月", @"四月", @"五月", @"六月", @"七月", @"八月",@"九月", @"十月", @"冬月", @"腊月"] 54 | #define ChineseDays @[@"初一", @"初二", @"初三", @"初四", @"初五", @"初六", @"初七", @"初八", @"初九", @"初十",@"十一", @"十二", @"十三", @"十四", @"十五", @"十六", @"十七", @"十八", @"十九", @"二十", @"廿一", @"廿二", @"廿三", @"廿四", @"廿五", @"廿六", @"廿七", @"廿八", @"廿九", @"三十"] 55 | 56 | typedef NS_ENUM(NSUInteger, FDCalendarMonth) { 57 | FDCalendarMonthPrevious = 0, 58 | FDCalendarMonthCurrent = 1, 59 | FDCalendarMonthNext = 2, 60 | }; 61 | 62 | @interface FDCalendarItem () 63 | 64 | @property (strong, nonatomic) UICollectionView *collectionView; 65 | 66 | @end 67 | 68 | @implementation FDCalendarItem 69 | 70 | - (instancetype)init { 71 | if (self = [super init]) { 72 | self.backgroundColor = [UIColor clearColor]; 73 | [self setupCollectionView]; 74 | [self setFrame:CGRectMake(0, 0, DeviceWidth, self.collectionView.frame.size.height + CollectionViewVerticalMargin * 2)]; 75 | } 76 | return self; 77 | } 78 | 79 | #pragma mark - Custom Accessors 80 | 81 | - (void)setDate:(NSDate *)date { 82 | _date = date; 83 | [self.collectionView reloadData]; 84 | } 85 | 86 | #pragma mark - Public 87 | 88 | // 获取date的下个月日期 89 | - (NSDate *)nextMonthDate { 90 | NSDateComponents *components = [[NSDateComponents alloc] init]; 91 | components.month = 1; 92 | NSDate *nextMonthDate = [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:self.date options:NSCalendarMatchStrictly]; 93 | return nextMonthDate; 94 | } 95 | 96 | // 获取date的上个月日期 97 | - (NSDate *)previousMonthDate { 98 | NSDateComponents *components = [[NSDateComponents alloc] init]; 99 | components.month = -1; 100 | NSDate *previousMonthDate = [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:self.date options:NSCalendarMatchStrictly]; 101 | return previousMonthDate; 102 | } 103 | 104 | #pragma mark - Private 105 | 106 | // collectionView显示日期单元,设置其属性 107 | - (void)setupCollectionView { 108 | CGFloat itemWidth = (DeviceWidth - CollectionViewHorizonMargin * 2) / 7; 109 | CGFloat itemHeight = itemWidth; 110 | 111 | UICollectionViewFlowLayout *flowLayot = [[UICollectionViewFlowLayout alloc] init]; 112 | flowLayot.sectionInset = UIEdgeInsetsZero; 113 | flowLayot.itemSize = CGSizeMake(itemWidth, itemHeight); 114 | flowLayot.minimumLineSpacing = 0; 115 | flowLayot.minimumInteritemSpacing = 0; 116 | 117 | CGRect collectionViewFrame = CGRectMake(CollectionViewHorizonMargin, CollectionViewVerticalMargin, DeviceWidth - CollectionViewHorizonMargin * 2, itemHeight * 6); 118 | self.collectionView = [[UICollectionView alloc] initWithFrame:collectionViewFrame collectionViewLayout:flowLayot]; 119 | [self addSubview:self.collectionView]; 120 | self.collectionView.dataSource = self; 121 | self.collectionView.delegate = self; 122 | self.collectionView.backgroundColor = [UIColor clearColor]; 123 | [self.collectionView registerClass:[FDCalendarCell class] forCellWithReuseIdentifier:@"CalendarCell"]; 124 | } 125 | 126 | // 获取date当前月的第一天是星期几 127 | - (NSInteger)weekdayOfFirstDayInDate { 128 | NSCalendar *calendar = [NSCalendar currentCalendar]; 129 | [calendar setFirstWeekday:1]; 130 | NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:self.date]; 131 | [components setDay:1]; 132 | NSDate *firstDate = [calendar dateFromComponents:components]; 133 | NSDateComponents *firstComponents = [calendar components:NSCalendarUnitWeekday fromDate:firstDate]; 134 | return firstComponents.weekday - 1; 135 | } 136 | 137 | // 获取date当前月的总天数 138 | - (NSInteger)totalDaysInMonthOfDate:(NSDate *)date { 139 | NSRange range = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date]; 140 | return range.length; 141 | } 142 | 143 | // 获取某月day的日期 144 | - (NSDate *)dateOfMonth:(FDCalendarMonth)calendarMonth WithDay:(NSInteger)day { 145 | NSCalendar *calendar = [NSCalendar currentCalendar]; 146 | NSDate *date; 147 | 148 | switch (calendarMonth) { 149 | case FDCalendarMonthPrevious: 150 | date = [self previousMonthDate]; 151 | break; 152 | 153 | case FDCalendarMonthCurrent: 154 | date = self.date; 155 | break; 156 | 157 | case FDCalendarMonthNext: 158 | date = [self nextMonthDate]; 159 | break; 160 | default: 161 | break; 162 | } 163 | 164 | NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date]; 165 | [components setDay:day]; 166 | NSDate *dateOfDay = [calendar dateFromComponents:components]; 167 | return dateOfDay; 168 | } 169 | 170 | // 获取date当天的农历 171 | - (NSString *)chineseCalendarOfDate:(NSDate *)date { 172 | NSString *day; 173 | NSCalendar *chineseCalendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierChinese]; 174 | NSDateComponents *components = [chineseCalendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date]; 175 | if (components.day == 1) { 176 | day = ChineseMonths[components.month - 1]; 177 | } else { 178 | day = ChineseDays[components.day - 1]; 179 | } 180 | 181 | return day; 182 | } 183 | 184 | #pragma mark - UICollectionDatasource 185 | 186 | - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 187 | return 42; 188 | } 189 | 190 | - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 191 | static NSString *identifier = @"CalendarCell"; 192 | FDCalendarCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 193 | cell.backgroundColor = [UIColor clearColor]; 194 | cell.dayLabel.textColor = [UIColor blackColor]; 195 | cell.chineseDayLabel.textColor = [UIColor grayColor]; 196 | NSInteger firstWeekday = [self weekdayOfFirstDayInDate]; 197 | NSInteger totalDaysOfMonth = [self totalDaysInMonthOfDate:self.date]; 198 | NSInteger totalDaysOfLastMonth = [self totalDaysInMonthOfDate:[self previousMonthDate]]; 199 | 200 | if (indexPath.row < firstWeekday) { // 小于这个月的第一天 201 | NSInteger day = totalDaysOfLastMonth - firstWeekday + indexPath.row + 1; 202 | cell.dayLabel.text = [NSString stringWithFormat:@"%ld", day]; 203 | cell.dayLabel.textColor = [UIColor grayColor]; 204 | cell.chineseDayLabel.text = [self chineseCalendarOfDate:[self dateOfMonth:FDCalendarMonthPrevious WithDay:day]]; 205 | } else if (indexPath.row >= totalDaysOfMonth + firstWeekday) { // 大于这个月的最后一天 206 | NSInteger day = indexPath.row - totalDaysOfMonth - firstWeekday + 1; 207 | cell.dayLabel.text = [NSString stringWithFormat:@"%ld", day]; 208 | cell.dayLabel.textColor = [UIColor grayColor]; 209 | cell.chineseDayLabel.text = [self chineseCalendarOfDate:[self dateOfMonth:FDCalendarMonthNext WithDay:day]]; 210 | } else { // 属于这个月 211 | NSInteger day = indexPath.row - firstWeekday + 1; 212 | cell.dayLabel.text= [NSString stringWithFormat:@"%ld", day]; 213 | 214 | if (day == [[NSCalendar currentCalendar] component:NSCalendarUnitDay fromDate:self.date]) { 215 | cell.backgroundColor = [UIColor redColor]; 216 | cell.layer.cornerRadius = cell.frame.size.height / 2; 217 | cell.dayLabel.textColor = [UIColor whiteColor]; 218 | cell.chineseDayLabel.textColor = [UIColor whiteColor]; 219 | } 220 | 221 | // 如果日期和当期日期同年同月不同天, 注:第一个判断中的方法是iOS8的新API, 会比较传入单元以及比传入单元大得单元上数据是否相等,亲测同时传入Year和Month结果错误 222 | if ([[NSCalendar currentCalendar] isDate:[NSDate date] equalToDate:self.date toUnitGranularity:NSCalendarUnitMonth] && ![[NSCalendar currentCalendar] isDateInToday:self.date]) { 223 | 224 | // 将当前日期的那天高亮显示 225 | if (day == [[NSCalendar currentCalendar] component:NSCalendarUnitDay fromDate:[NSDate date]]) { 226 | cell.dayLabel.textColor = [UIColor redColor]; 227 | } 228 | } 229 | 230 | cell.chineseDayLabel.text = [self chineseCalendarOfDate:[self dateOfMonth:FDCalendarMonthCurrent WithDay:day]]; 231 | } 232 | 233 | 234 | 235 | return cell; 236 | } 237 | 238 | #pragma mark - UICollectionViewDelegate 239 | 240 | - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { 241 | NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:self.date]; 242 | NSInteger firstWeekday = [self weekdayOfFirstDayInDate]; 243 | [components setDay:indexPath.row - firstWeekday + 1]; 244 | NSDate *selectedDate = [[NSCalendar currentCalendar] dateFromComponents:components]; 245 | if (self.delegate && [self.delegate respondsToSelector:@selector(calendarItem:didSelectedDate:)]) { 246 | [self.delegate calendarItem:self didSelectedDate:selectedDate]; 247 | } 248 | } 249 | 250 | @end 251 | -------------------------------------------------------------------------------- /FDCalendarDemo/FDCalendarDemo/Images.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "ipad", 35 | "size" : "29x29", 36 | "scale" : "1x" 37 | }, 38 | { 39 | "idiom" : "ipad", 40 | "size" : "29x29", 41 | "scale" : "2x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "40x40", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "40x40", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "76x76", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "76x76", 61 | "scale" : "2x" 62 | } 63 | ], 64 | "info" : { 65 | "version" : 1, 66 | "author" : "xcode" 67 | } 68 | } -------------------------------------------------------------------------------- /FDCalendarDemo/FDCalendarDemo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | com.fergus.$(PRODUCT_NAME:rfc1034identifier) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIViewControllerBasedStatusBarAppearance 30 | 31 | UIRequiredDeviceCapabilities 32 | 33 | armv7 34 | 35 | UISupportedInterfaceOrientations 36 | 37 | UIInterfaceOrientationPortrait 38 | UIInterfaceOrientationLandscapeLeft 39 | UIInterfaceOrientationLandscapeRight 40 | 41 | UISupportedInterfaceOrientations~ipad 42 | 43 | UIInterfaceOrientationPortrait 44 | UIInterfaceOrientationPortraitUpsideDown 45 | UIInterfaceOrientationLandscapeLeft 46 | UIInterfaceOrientationLandscapeRight 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /FDCalendarDemo/FDCalendarDemo/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // FDCalendarDemo 4 | // 5 | // Created by fergusding on 15/8/20. 6 | // Copyright (c) 2015年 fergusding. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /FDCalendarDemo/FDCalendarDemo/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // FDCalendarDemo 4 | // 5 | // Created by fergusding on 15/8/20. 6 | // Copyright (c) 2015年 fergusding. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | #import "FDCalendar.h" 11 | 12 | @interface ViewController () 13 | 14 | @end 15 | 16 | @implementation ViewController 17 | 18 | - (void)viewDidLoad { 19 | [super viewDidLoad]; 20 | [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; 21 | 22 | FDCalendar *calendar = [[FDCalendar alloc] initWithCurrentDate:[NSDate date]]; 23 | CGRect frame = calendar.frame; 24 | frame.origin.y = 20; 25 | calendar.frame = frame; 26 | [self.view addSubview:calendar]; 27 | } 28 | 29 | - (void)didReceiveMemoryWarning { 30 | [super didReceiveMemoryWarning]; 31 | } 32 | 33 | @end 34 | -------------------------------------------------------------------------------- /FDCalendarDemo/FDCalendarDemo/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // FDCalendarDemo 4 | // 5 | // Created by fergusding on 15/8/20. 6 | // Copyright (c) 2015年 fergusding. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /FDCalendarDemo/FDCalendarDemoTests/FDCalendarDemoTests.m: -------------------------------------------------------------------------------- 1 | // 2 | // FDCalendarDemoTests.m 3 | // FDCalendarDemoTests 4 | // 5 | // Created by fergusding on 15/8/20. 6 | // Copyright (c) 2015年 fergusding. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | 12 | @interface FDCalendarDemoTests : XCTestCase 13 | 14 | @end 15 | 16 | @implementation FDCalendarDemoTests 17 | 18 | - (void)setUp { 19 | [super setUp]; 20 | // Put setup code here. This method is called before the invocation of each test method in the class. 21 | } 22 | 23 | - (void)tearDown { 24 | // Put teardown code here. This method is called after the invocation of each test method in the class. 25 | [super tearDown]; 26 | } 27 | 28 | - (void)testExample { 29 | // This is an example of a functional test case. 30 | XCTAssert(YES, @"Pass"); 31 | } 32 | 33 | - (void)testPerformanceExample { 34 | // This is an example of a performance test case. 35 | [self measureBlock:^{ 36 | // Put the code you want to measure the time of here. 37 | }]; 38 | } 39 | 40 | @end 41 | -------------------------------------------------------------------------------- /FDCalendarDemo/FDCalendarDemoTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | com.fergus.$(PRODUCT_NAME:rfc1034identifier) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /FDCalendarDemo/Resource/icon_next@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fergusding/FDCalendar/9eb25f06375f444a65183d7f49f8f543ec09c16c/FDCalendarDemo/Resource/icon_next@2x.png -------------------------------------------------------------------------------- /FDCalendarDemo/Resource/icon_previous@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fergusding/FDCalendar/9eb25f06375f444a65183d7f49f8f543ec09c16c/FDCalendarDemo/Resource/icon_previous@2x.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # FDCalendar 2 | A custom calendar control in iOS 3 | 4 | # Introduction 5 | 6 | FDCalendar is a custom calendar control which includes world and chinese calendar date. It supports scrolling by left and right to jump to previous or next month date, also it can jump to the date which is selected. Otherwise it seems beautiful and good using. If you want to change some style of it, you need to fork it and modify the properties by yourself. If you think it is helpful, please star it. 7 | 8 | ## Preview 9 | 10 | ![preview](http://7xiamc.com1.z0.glb.clouddn.com/preview.gif) 11 | 12 | ## ScreenShot 13 | 14 | ![one](http://7xiamc.com1.z0.glb.clouddn.com/screenshot1.png) 15 | ![two](http://7xiamc.com1.z0.glb.clouddn.com/screenshot2.png) 16 | ![two](http://7xiamc.com1.z0.glb.clouddn.com/screenshot3.png) 17 | 18 | # Get started 19 | 20 | 1. Download the source file in the folder FDCalendar. 21 | 2. Add the FDCalendar.h and FDCalendar.m files to your project. 22 | 3. Import the FDCalendar.h file where you want to use it. Jsut add a instance of it to your view as a subview. 23 | 24 | # Usage 25 | 26 | ## FDCalendar 27 | 28 | - The instance of it is init with a date, and you don't have to set it's frame but change it's frame's origin to show it at th position you like. 29 | 30 | ```Objective-C 31 | FDCalendar *calendar = [[FDCalendar alloc] initWithCurrentDate:[NSDate date]]; 32 | CGRect frame = calendar.frame; 33 | frame.origin.y = 20; 34 | calendar.frame = frame; 35 | [self.view addSubview:calendar]; 36 | ``` 37 | 38 | #License 39 | MIT 40 | -------------------------------------------------------------------------------- /ScreenShot/screenshot1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fergusding/FDCalendar/9eb25f06375f444a65183d7f49f8f543ec09c16c/ScreenShot/screenshot1.png -------------------------------------------------------------------------------- /ScreenShot/screenshot2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fergusding/FDCalendar/9eb25f06375f444a65183d7f49f8f543ec09c16c/ScreenShot/screenshot2.png -------------------------------------------------------------------------------- /ScreenShot/screenshot3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fergusding/FDCalendar/9eb25f06375f444a65183d7f49f8f543ec09c16c/ScreenShot/screenshot3.png -------------------------------------------------------------------------------- /preview.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fergusding/FDCalendar/9eb25f06375f444a65183d7f49f8f543ec09c16c/preview.gif --------------------------------------------------------------------------------