Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加floatingmode下 自定义footerheight的高度,这样可以让日历日期和年月的展示间距可以自己调整 #1412

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions Example-Objc/FSCalendar.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@
CLANG_ANALYZER_NONNULL = YES;
DEBUG_INFORMATION_FORMAT = dwarf;
INFOPLIST_FILE = FSCalendarTests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 9.3;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.wenchao.FSCalendarTests;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -769,7 +769,7 @@
BUNDLE_LOADER = "$(TEST_HOST)";
CLANG_ANALYZER_NONNULL = YES;
INFOPLIST_FILE = FSCalendarTests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 9.3;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.wenchao.FSCalendarTests;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down Expand Up @@ -825,7 +825,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand Down Expand Up @@ -873,7 +873,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
VALIDATE_PRODUCT = YES;
Expand All @@ -886,7 +886,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = HZF422TY46;
INFOPLIST_FILE = "$(SRCROOT)/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.wenchaod.FSCalendarExample;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -900,7 +900,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = HZF422TY46;
INFOPLIST_FILE = "$(SRCROOT)/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.wenchaod.FSCalendarExample;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -923,7 +923,7 @@
);
INFOPLIST_FILE = "$(SRCROOT)/../FSCalendar/Info.plist";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MARKETING_VERSION = 2.8.4;
PRODUCT_BUNDLE_IDENTIFIER = com.wenchaod.FSCalendar;
Expand All @@ -946,7 +946,7 @@
DYLIB_INSTALL_NAME_BASE = "@rpath";
INFOPLIST_FILE = "$(SRCROOT)/../FSCalendar/Info.plist";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MARKETING_VERSION = 2.8.4;
PRODUCT_BUNDLE_IDENTIFIER = com.wenchaod.FSCalendar;
Expand All @@ -970,7 +970,7 @@
"$(inherited)",
);
INFOPLIST_FILE = FSCalendarExampleUITests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 10.2;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.wenchaod.FSCalendarExampleUITests;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -985,7 +985,7 @@
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
DEVELOPMENT_TEAM = HZF422TY46;
INFOPLIST_FILE = FSCalendarExampleUITests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 10.2;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.wenchaod.FSCalendarExampleUITests;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down
42 changes: 40 additions & 2 deletions Example-Objc/RangePickerViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ @interface RangePickerViewController () <FSCalendarDataSource,FSCalendarDelegate

- (void)configureCell:(__kindof FSCalendarCell *)cell forDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)position;

@property (strong, nonatomic) NSDate *minDate;
@property (strong, nonatomic) NSDate *maxDate;

@end

@implementation RangePickerViewController
Expand All @@ -51,6 +54,8 @@ - (void)loadView
calendar.pagingEnabled = NO;
calendar.allowsMultipleSelection = YES;
calendar.rowHeight = 60;
calendar.footerHeight = 50;
calendar.floatingModeNotDisplayNonVisibleAreasDate = YES;
calendar.placeholderType = FSCalendarPlaceholderTypeNone;
[view addSubview:calendar];
self.calendar = calendar;
Expand Down Expand Up @@ -80,6 +85,8 @@ - (void)viewDidLoad

// For UITest
self.calendar.accessibilityIdentifier = @"calendar";
self.minDate = [self.gregorian dateByAddingUnit:NSCalendarUnitMonth value:-36 toDate:[NSDate date] options:0];
self.maxDate = [NSDate date];
}

- (void)dealloc
Expand All @@ -91,12 +98,14 @@ - (void)dealloc

- (NSDate *)minimumDateForCalendar:(FSCalendar *)calendar
{
return [self.dateFormatter dateFromString:@"2020-07-08"];
// return [self.dateFormatter dateFromString:@"2020-07-08"];
return self.minDate;
}

- (NSDate *)maximumDateForCalendar:(FSCalendar *)calendar
{
return [self.gregorian dateByAddingUnit:NSCalendarUnitMonth value:10 toDate:[NSDate date] options:0];
// return [self.gregorian dateByAddingUnit:NSCalendarUnitMonth value:10 toDate:[NSDate date] options:0];
return self.maxDate;
}

- (NSString *)calendar:(FSCalendar *)calendar titleForDate:(NSDate *)date
Expand Down Expand Up @@ -173,6 +182,35 @@ - (void)calendar:(FSCalendar *)calendar didDeselectDate:(NSDate *)date atMonthPo
return @[appearance.eventDefaultColor];
}


- (nullable UIColor *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance titleDefaultColorForDate:(NSDate *)date {
if([self outofRangeDate:date]){
UIColor *color;
if(self.calendar.floatingModeNotDisplayNonVisibleAreasDate){
color = [UIColor clearColor];
}else {
color = [UIColor grayColor];
}
return color;
}

UIColor *subColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1.0];

return subColor;
}

- (BOOL)outofRangeDate:(NSDate *)date {
BOOL outof = NO;
if(([self.gregorian compareDate:date toDate:self.minDate toUnitGranularity:NSCalendarUnitDay] == NSOrderedAscending) ||
([self.gregorian compareDate:date toDate:self.maxDate toUnitGranularity:NSCalendarUnitDay] == NSOrderedDescending)){
outof = YES;
}
return outof;
}




#pragma mark - Private methods

- (void)configureVisibleCells
Expand Down
9 changes: 9 additions & 0 deletions FSCalendar/FSCalendar.h
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,15 @@ IB_DESIGNABLE
*/
@property (assign, nonatomic) IBInspectable CGFloat headerHeight;

/**
The height of month footer of the calendar. Give a '0' to remove the footer.
*/
@property (assign, nonatomic) IBInspectable CGFloat footerHeight;


//不展示当月不在日历区域的日期时间
@property (assign, nonatomic) BOOL floatingModeNotDisplayNonVisibleAreasDate;

/**
The height of weekday header of the calendar.
*/
Expand Down
25 changes: 25 additions & 0 deletions FSCalendar/FSCalendar.m
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ - (void)initialize
[collectionView registerClass:[FSCalendarCell class] forCellWithReuseIdentifier:FSCalendarDefaultCellReuseIdentifier];
[collectionView registerClass:[FSCalendarBlankCell class] forCellWithReuseIdentifier:FSCalendarBlankCellReuseIdentifier];
[collectionView registerClass:[FSCalendarStickyHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];

[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];

[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"placeholderHeader"];
[daysContainer addSubview:collectionView];
self.collectionView = collectionView;
Expand Down Expand Up @@ -446,6 +449,11 @@ - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
[stickyHeader setNeedsLayout];
return stickyHeader;
}


if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
return [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];
}
}
return [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"placeholderHeader" forIndexPath:indexPath];
}
Expand Down Expand Up @@ -812,6 +820,23 @@ - (void)setWeekdayHeight:(CGFloat)weekdayHeight
}
}

- (void)setFooterHeight:(CGFloat)footerHeight
{
if (_footerHeight != footerHeight) {
_footerHeight = footerHeight;
_needsAdjustingViewFrame = YES;
[self setNeedsLayout];
}
}

- (void)setFloatingModeNotDisplayNonVisibleAreasDate:(BOOL)floatingModeNotDisplayNonVisibleAreasDate {
if (_floatingModeNotDisplayNonVisibleAreasDate != floatingModeNotDisplayNonVisibleAreasDate) {
_floatingModeNotDisplayNonVisibleAreasDate = floatingModeNotDisplayNonVisibleAreasDate;
_needsAdjustingViewFrame = YES;
[self setNeedsLayout];
}
}

- (void)setLocale:(NSLocale *)locale
{
if (![_locale isEqual:locale]) {
Expand Down
73 changes: 66 additions & 7 deletions FSCalendar/FSCalendarCalculator.m
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ - (NSIndexPath *)indexPathForDate:(NSDate *)date atMonthPosition:(FSCalendarMont
NSInteger section = 0;
switch (scope) {
case FSCalendarScopeMonth: {
section = [self.gregorian components:NSCalendarUnitMonth fromDate:[self.gregorian fs_firstDayOfMonth:self.minimumDate] toDate:[self.gregorian fs_firstDayOfMonth:date] options:0].month;
section = [self.gregorian components:NSCalendarUnitMonth fromDate:[self.gregorian fs_firstDayOfMonth:self.minimumDate] toDate:[self.gregorian fs_firstDayOfMonth:date] options:0].month;//这里只是为了判断当前的日期月份跟最小日期的月份之间的月份差有多少来判断section位置 所以不用调整 [self.gregorian fs_firstDayOfMonth:self.minimumDate] 为 [self currentMiniDate]
if (position == FSCalendarMonthPositionPrevious) {
section++;
} else if (position == FSCalendarMonthPositionNext) {
Expand Down Expand Up @@ -162,21 +162,47 @@ - (NSDate *)monthForSection:(NSInteger)section
NSNumber *key = @(section);
NSDate *month = self.months[key];
if (!month) {
month = [self.gregorian dateByAddingUnit:NSCalendarUnitMonth value:section toDate:[self.gregorian fs_firstDayOfMonth:self.minimumDate] options:0];
NSInteger numberOfHeadPlaceholders = [self numberOfHeadPlaceholdersForMonth:month];
NSDate *monthHead = [self.gregorian dateByAddingUnit:NSCalendarUnitDay value:-numberOfHeadPlaceholders toDate:month options:0];
month = [self.gregorian dateByAddingUnit:NSCalendarUnitMonth value:section toDate:[self currentMiniDate] options:0];

NSInteger numberOfHeadPlaceholders;
NSDate *monthHead;
if(self.calendar.floatingModeNotDisplayNonVisibleAreasDate){
if([self isInSameMonth:month time2:self.minimumDate]){
numberOfHeadPlaceholders = [self numberOfHeadPlaceholdersForMonth:month];
monthHead = [self.gregorian dateByAddingUnit:NSCalendarUnitDay value:-numberOfHeadPlaceholders toDate:month options:0];
}else {
NSDate *firstDate = [self.gregorian fs_firstDayOfMonth:month];
numberOfHeadPlaceholders = [self numberOfHeadPlaceholdersForMonth:firstDate];
monthHead = [self.gregorian dateByAddingUnit:NSCalendarUnitDay value:-numberOfHeadPlaceholders toDate:firstDate options:0];
month = firstDate;
}
}else {
numberOfHeadPlaceholders = [self numberOfHeadPlaceholdersForMonth:month];
monthHead = [self.gregorian dateByAddingUnit:NSCalendarUnitDay value:-numberOfHeadPlaceholders toDate:month options:0];
}

self.months[key] = month;
self.monthHeads[key] = monthHead;
}
return month;
}

- (NSDate *)currentMiniDate {
NSDate *miniDate;
if(self.calendar.floatingModeNotDisplayNonVisibleAreasDate){
miniDate = self.minimumDate;
}else {
miniDate = [self.gregorian fs_firstDayOfMonth:self.minimumDate];
}
return miniDate;
}

- (NSDate *)monthHeadForSection:(NSInteger)section
{
NSNumber *key = @(section);
NSDate *monthHead = self.monthHeads[key];
if (!monthHead) {
NSDate *month = [self.gregorian dateByAddingUnit:NSCalendarUnitMonth value:section toDate:[self.gregorian fs_firstDayOfMonth:self.minimumDate] options:0];
NSDate *month = [self.gregorian dateByAddingUnit:NSCalendarUnitMonth value:section toDate:[self currentMiniDate] options:0];
NSInteger numberOfHeadPlaceholders = [self numberOfHeadPlaceholdersForMonth:month];
monthHead = [self.gregorian dateByAddingUnit:NSCalendarUnitDay value:-numberOfHeadPlaceholders toDate:month options:0];
self.months[key] = month;
Expand Down Expand Up @@ -224,7 +250,18 @@ - (NSInteger)numberOfRowsInMonth:(NSDate *)month
if (rowCount == nil) {
NSDate *firstDayOfMonth = [self.gregorian fs_firstDayOfMonth:month];
NSInteger weekdayOfFirstDay = [self.gregorian component:NSCalendarUnitWeekday fromDate:firstDayOfMonth];
NSInteger numberOfDaysInMonth = [self.gregorian fs_numberOfDaysInMonth:month];
NSInteger numberOfDaysInMonth;
if(self.calendar.floatingModeNotDisplayNonVisibleAreasDate){
if([self isInSameMonth:month time2:self.minimumDate]){
numberOfDaysInMonth = [self.gregorian fs_numberOfDaysInMonth:month currentDay:self.minimumDate isRest:YES];
}else if([self isInSameMonth:month time2:self.maximumDate]){
numberOfDaysInMonth = [self.gregorian fs_numberOfDaysInMonth:month currentDay:self.maximumDate isRest:NO];
}else {
numberOfDaysInMonth = [self.gregorian fs_numberOfDaysInMonth:month];
}
}else {
numberOfDaysInMonth = [self.gregorian fs_numberOfDaysInMonth:month];
}
NSInteger numberOfPlaceholdersForPrev = ((weekdayOfFirstDay - self.gregorian.firstWeekday) + 7) % 7;
NSInteger headDayCount = numberOfDaysInMonth + numberOfPlaceholdersForPrev;
NSInteger numberOfRows = (headDayCount/7) + (headDayCount%7>0);
Expand All @@ -234,6 +271,28 @@ - (NSInteger)numberOfRowsInMonth:(NSDate *)month
return rowCount.integerValue;
}

/**
是否属于同一月

@param time1 time1
@param time2 time2
@return 同一月为真,否则为假
*/
- (BOOL)isInSameMonth:(NSDate *)time1 time2:(NSDate *)time2
{
NSDate *date1 = time1;
NSDate *date2 = time2;

NSCalendar *calendar = [NSCalendar currentCalendar];
int unit = NSCalendarUnitMonth | NSCalendarUnitYear;

NSDateComponents *nowCmps = [calendar components:unit fromDate:date1];
NSDateComponents *selfCmps = [calendar components:unit fromDate:date2];

return (nowCmps.year == selfCmps.year)&&(nowCmps.month == selfCmps.month);
}


- (NSInteger)numberOfRowsInSection:(NSInteger)section
{
if (self.calendar.transitionCoordinator.representingScope == FSCalendarScopeWeek) return 1;
Expand Down Expand Up @@ -270,7 +329,7 @@ - (FSCalendarCoordinate)coordinateForIndexPath:(NSIndexPath *)indexPath

- (void)reloadSections
{
self.numberOfMonths = [self.gregorian components:NSCalendarUnitMonth fromDate:[self.gregorian fs_firstDayOfMonth:self.minimumDate] toDate:self.maximumDate options:0].month+1;
self.numberOfMonths = [self.gregorian components:NSCalendarUnitMonth fromDate:[self currentMiniDate] toDate:self.maximumDate options:0].month+1;
self.numberOfWeeks = [self.gregorian components:NSCalendarUnitWeekOfYear fromDate:[self.gregorian fs_firstDayOfWeek:self.minimumDate] toDate:self.maximumDate options:0].weekOfYear+1;
[self clearCaches];
}
Expand Down
Loading