[FSCalendar] 可定制的iOS日历组件 | docs | Interface Builder

链接:WenchaoD/FSCalendar: A fully customizable iOS calendar library, compatible with Objective-C and Swift

(ipad上已经diy用上了,邻居家的小孩很喜欢吃😋)

docs:FSCalendar日历组件

FSCalendar是一款功能强大且高度可定制的iOS日历库,能够帮助开发者轻松实现日历视图集成

提供灵活的网格布局 展示日期,支持日期选择 等交互操作,并具备丰富的样式定制动画效果,可流畅切换月/周视图模式。

可视化

章节

  1. FSCalendarCell (日期单元)
  2. 日历标题组件
  3. FSCalendar (主日历)
  4. FSCalendarAppearance (样式指南)
  5. FSCalendarDelegationProxy (代理中转)
  6. 日历集合视图网格
  7. FSCalendarCalculator (日期计算器)
  8. FSCalendarTransitionCoordinator (转场协调器)

(文档代码之后有机会窝给它更出来(下次一定bush🕳+1)

目录

截图展示

iPhone效果

iPad效果

安全方向适配

今日扩展

iOS8/9 | iOS10

交互式范围手势

自定义支持

通过DIY示例自定义单元格样式(参见Example-Swift或Example-Objc)

滑动选择演示

单选模式 | 多选模式 | DIY模式

用户成果展示

更多作品详见FSCalendar图库

安装指南

CocoaPods

iOS8+推荐:

ruby 复制代码
use_frameworks!
target '项目名称' do
    pod 'FSCalendar'
end

iOS7+兼容:

ruby 复制代码
target '项目名称' do
    pod 'FSCalendar'
end

需配合NSCalendarExtension使用

Carthage

iOS8+:
github "WenchaoD/FSCalendar"

SPM

添加依赖:
.package(url: "https://github.com/WenchaoD/FSCalendar.git", from: "2.8.4")

手动集成

将FSCalendar文件夹所有文件拖入工程(支持IB设计)

或直接运行Example-Objc/Example-Swift中的UITest Target

配置方法

Interface Builder方式

  1. 添加UIView到ViewController场景
  2. 修改Custom Class为FSCalendar
  3. 关联dataSource和delegate
  4. 实现FSCalendarDataSource和FSCalendarDelegate协议

代码方式

Objective-C:

objc 复制代码
@property (weak, nonatomic) FSCalendar *calendar;
// 在loadView或viewDidLoad中
FSCalendar *calendar = [[FSCalendar alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
calendar.dataSource = self;
calendar.delegate = self;
[self.view addSubview:calendar];
self.calendar = calendar;

Swift:

swift 复制代码
private weak var calendar: FSCalendar!
// 在loadView或viewDidLoad中
let calendar = FSCalendar(frame: CGRect(x: 0, y: 0, width: 320, height: 300))
calendar.dataSource = self
calendar.delegate = self
view.addSubview(calendar)
self.calendar = calendar

Swift3需创建Bridge Header,详见Example-Swift

重要提示

FSCalendar不会自动更新frame,需实现以下方法:

自动布局:

objc 复制代码
- (void)calendar:(FSCalendar *)calendar boundingRectWillChange:(CGRect)bounds animated:(BOOL)animated {
    self.calendarHeightConstraint.constant = bounds.size.height;
    [self.view layoutIfNeeded];
}

手动布局:

objc 复制代码
calendar.frame = (CGRect){calendar.frame.origin, bounds.size};

Masonry:

objc 复制代码
[calendar mas_updateConstraints:^(MASConstraintMaker *make) {
    make.height.equalTo(@(bounds.size.height));
}];

SnapKit:

swift 复制代码
calendar.snp.updateConstraints { make in
    make.height.equalTo(bounds.height)
}

基础须知

Swift3中NSDate和NSDateFormatter已重命名为Date和DateFormatter

日期对象操作

创建NSDate:

objc 复制代码
// 通过日历
self.gregorian = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
NSDate *date = [gregorian dateWithEra:1 year:2016 month:9 day:10 hour:0 minute:0 second:0 nanosecond:0];

// 通过格式化
self.formatter = [[NSDateFormatter alloc] init];
self.formatter.dateFormat = @"yyyy-MM-dd";
NSDate *date = [self.formatter dateFromString:@"2016-09-10"];

日期计算:

objc 复制代码
// 获取下个月/天
NSDate *nextMonth = [gregorian dateByAddingUnit:NSCalendarUnitMonth value:1 toDate:date options:0];
NSDate *nextDay = [gregorian dateByAddingUnit:NSCalendarUnitDay value:1 toDate:date options:0];

// 日期判断
BOOL isToday = [gregorian isDateInToday:date];
BOOL isWeekend = [gregorian isDateInWeekend:date];

// 日期比较
BOOL sameDay = [gregorian isDate:date1 inSameDayAsDate:date2];
NSComparisonResult result = [gregorian compareDate:date1 toDate:date2 toUnitGranularity:NSCalendarUnitDay];
相关推荐
1024小神1 天前
xcode 配置了AppIcon 但是不显示icon图标
ios·swiftui·swift
2501_915918411 天前
iOS 项目中证书管理常见的协作问题
android·ios·小程序·https·uni-app·iphone·webview
ITKEY_1 天前
iOS网页应用无地址栏无工具栏
ios
2501_915918411 天前
提升 iOS 应用安全审核通过率的一种思路,把容易被拒的点先处理
android·安全·ios·小程序·uni-app·iphone·webview
RollingPin1 天前
iOS探究使用Block方式实现一对多回调能力
ios·block·runtime·数据分发·解耦·动态绑定·一对多回调
TheNextByte11 天前
iPhone短信备份与恢复:3种最佳方法及短信备份与恢复应用
ios·iphone
2501_916008891 天前
iOS 应用发布流程中常被忽视的关键环节
android·ios·小程序·https·uni-app·iphone·webview
ForteScarlet1 天前
Kotlin 2.3.0 现已发布!又有什么好东西?
android·开发语言·后端·ios·kotlin
往来凡尘2 天前
Flutter运行iOS26真机的两个问题
flutter·ios
普通网友2 天前
Objective-C 类的方法重载与重写:区别与正确使用场景
开发语言·ios·objective-c