iOS日历视图 FSCalendar的使用
由于在项目中需要使用到日历视图,无意中发现了一款开源的日历控件FSCalendar,在此介绍一下FSCalendar的使用.一、安装1.使用CocoaPods安装在pod file中添加如下代码:use_frameworks!pod 'FSCalendar'2.手动安装直接将FSCalendar文件夹拖进你的项目中,这样就可以使用了二、FSCalendar的使用在需要使用日
文章共963字 · 阅读需要大约4分钟
一键AI生成摘要,助你高效阅读
问答
·
由于在项目中需要使用到日历视图,无意中发现了一款开源的日历控件FSCalendar,在此介绍一下FSCalendar的使用.
一、安装
1.使用CocoaPods安装
在pod file中添加如下代码:
use_frameworks!
pod 'FSCalendar'
2.手动安装
直接将FSCalendar文件夹拖进你的项目中,这样就可以使用了
二、FSCalendar的使用
在需要使用日历的类中直接导入头文件
#import "FSCalendar-umbrella.h"
遵守以下协议:
FSCalendarDataSource,
FSCalendarDelegate,
FSCalendarDelegateAppearance
创建FSCalendar:
@property (nonatomic, strong) FSCalendar *calendar;
- (FSCalendar *)calendar {
if (!_calendar) {
_calendar = [[FSCalendar alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 248)];
_calendar.dataSource = self;
_calendar.delegate = self;
//设置翻页方式为水平
_calendar.scrollDirection = FSCalendarScrollDirectionHorizontal;
//设置是否用户多选
_calendar.allowsMultipleSelection = NO;
_calendar.appearance.caseOptions = FSCalendarCaseOptionsHeaderUsesUpperCase|FSCalendarCaseOptionsWeekdayUsesSingleUpperCase;
//这个属性控制"上个月"和"下个月"标签在静止时刻的透明度 _calendar.appearance.headerMinimumDissolvedAlpha = 0;
_calendar.backgroundColor = [ToolHelper colorWithHexString:@"#368de7"];
//设置周字体颜色
_calendar.appearance.weekdayTextColor = [UIColor whiteColor];
//设置头字体颜色
_calendar.appearance.headerTitleColor = [UIColor whiteColor];
//创建点击跳转显示上一月和下一月button
UIButton *previousButton = [UIButton buttonWithType:UIButtonTypeCustom];
previousButton.frame = CGRectMake(self.view.centerX - 50 - 6.5, 13, 6.5, 13);
previousButton.titleLabel.font = [UIFont systemFontOfSize:15];
[previousButton setImage:[UIImage imageNamed:@"backicon"] forState:UIControlStateNormal];
[previousButton addTarget:self action:@selector(previousClicked:) forControlEvents:UIControlEventTouchUpInside];
[_calendar addSubview:previousButton];
UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
nextButton.frame = CGRectMake(self.view.centerX + 50, 13, 6.5, 13);
nextButton.titleLabel.font = [UIFont systemFontOfSize:15];
[nextButton setImage:[UIImage imageNamed:@"backicon"] forState:UIControlStateNormal];
nextButton.imageView.transform = CGAffineTransformMakeRotation(M_PI);
[nextButton addTarget:self action:@selector(nextClicked:) forControlEvents:UIControlEventTouchUpInside];
[_calendar addSubview:nextButton];
//设置当天的字体颜色
_calendar.todayColor = COLOR_BLUE;
}
return _calendar;
}
//上一月按钮点击事件
- (void)previousClicked:(id)sender {
NSDate *currentMonth = self.calendar.currentPage;
NSDate *previousMonth = [self.calendar dateBySubstractingMonths:1 fromDate:currentMonth];
[self.calendar setCurrentPage:previousMonth animated:YES];
}
//下一月按钮点击事件
- (void)nextClicked:(id)sender {
NSDate *currentMonth = self.calendar.currentPage;
NSDate *nextMonth = [self.calendar dateByAddingMonths:1 toDate:currentMonth];
[self.calendar setCurrentPage:nextMonth animated:YES];
}
相关属性说明:
1._calendar.appearance.caseOptions
// 定义
typedef NS_OPTIONS(NSUInteger, FSCalendarCaseOptions) {
// 月文字(如果手机系统字体为中文,那么以下两种选项设置任何一种都是中文显示;如果为英文则按照以下相应的设置生效)
FSCalendarCaseOptionsHeaderUsesDefaultCase = 0, // June 2016
FSCalendarCaseOptionsHeaderUsesUpperCase = 1, // JUNE 2016
// 周文字 (如果手机系统为中文,设置则按中文生效,为英文则按照英文生效)
FSCalendarCaseOptionsWeekdayUsesDefaultCase = 0 << 4, // Sun、Mon、Tue、Wed、Thu、Fri、Sat; 周日、周一、周二、周三、周四、周五、周六
FSCalendarCaseOptionsWeekdayUsesUpperCase = 1 << 4, // SUN、MON、TUE、WED、THU、FRI、SAT; 周日、周一、周二、周三、周四、周五、周六
FSCalendarCaseOptionsWeekdayUsesSingleUpperCase = 2 << 4, // S、M、T、W、T、F、S; 日、一、二、三、四、五、六
};
FSCalendarAppearance:用于全局外观属性的定制。
FSCalendarDelegateAppearance:针对某天外观属性的定制。
#pragma mark FSCalendarDelegateAppearance
//设置当前月与非当前月字体颜色
- (UIColor *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance titleDefaultColorForDate:(NSDate *)date {
if ([calendar date:[NSDate date] sharesSameMonthWithDate:date]) {
return [UIColor whiteColor];
} else {
return [ToolHelper colorWithHexString:@"#8cc0f5"];//[UIColor colorWithWhite:0.702 alpha:1.000]
}
}
//设置选中日期与未选中日期Title的颜色
- (UIColor *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance titleSelectionColorForDate:(NSDate *)date {
if (_dateArray.count > 0) {
for (NSDate *obj in _dateArray) {
if ([_calendar date:obj sharesSameDayWithDate:date]) {
return COLOR_BLUE;
}
}
}
return [UIColor whiteColor];
}
//设置选中日期与未选中日期的填充色
- (UIColor *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance selectionColorForDate:(NSDate *)date {
if (_dateArray.count > 0) {
for (NSDate *obj in _dateArray) {
if ([_calendar date:obj sharesSameDayWithDate:date]) {
return [UIColor whiteColor];
}
}
}
return COLOR_BLUE;
}
//设置可选日期与不可选日期的border颜色
- (UIColor *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance borderDefaultColorForDate:(NSDate *)date {
if (_dateArray.count > 0) {
for (NSDate *obj in _dateArray) {
if ([_calendar date:obj sharesSameDayWithDate:date]) {
return COLOR_YELLOW;
}
}
}
return COLOR_BLUE;
}
//设置选中日期的border颜色
- (UIColor *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance borderSelectionColorForDate:(NSDate *)date {
return [UIColor whiteColor];
}
//设置可选日期
- (BOOL)calendar:(FSCalendar *)calendar shouldSelectDate:(NSDate *)date {
if (_dateArray.count > 0) {
for (NSDate *obj in _dateArray) {
if ([_calendar date:obj sharesSameDayWithDate:date]) {
return YES;
}
}
}
return NO;
}
#pragma mark FSCalendarDelegate
//选中某一天进行相关操作
- (void)calendar:(FSCalendar *)calendar didSelectDate:(NSDate *)date {
//...
}
//取消选中的日期进行相关操作
- (void)calendar:(FSCalendar *)calendar didDeselectDate:(NSDate *)date {
//...
}
*如何去除底部分割线:
可以通过修改bottomBorder的相关属性设置底部分割线
//隐藏底部分割线
_calendar.bottomBorder.hidden = YES;
更多推荐
已为社区贡献1条内容
所有评论(0)