Compact Calendar
Compact Calendar是一个使用 Swift 开发,带有可自定义视图的 CocoaPod,每次显示两周的日历。
安装
- 将以下行插入您的 Podfile 中的
target $YOUR_PROJECT_NAME do
下
pod 'CompactCalendar'
-
运行
pod install
-
打开您的项目工作区
设置 Compact Calendar
- 在您的 storyboard 中添加一个视图到所需的 View Controller
- 在 View Controller 中放置视图,添加所需的约束,并设置高度为 165。这是为了确保所有内容都能按预期放入日历中。
- 在 storyboard 中将视图的类从 UIView 更改为 CompactCalendar,在自定义类别下
- 将 Compact Calendar 添加为您的 View Controller 类中的 IBOutlet
在您的 View Controller 的 Swift 文件中
- 在文件顶部导入 CompactCalendar
import UIKit
import CompactCalendar
- 让你的视图控制器遵从
CompactCalendarDelegate
并添加所需的函数
class MyViewController: UIViewController, CompactCalendarDelegate {
@IBOutlet weak var compactCalendar: CompactCalendar!
func compactCalendar(_ compactCalendar: CompactCalendar, didSelectCalendarCellWith date: Date, isAlreadySelected: Bool) {
}
func didGoToNextPage(_ compactCalendar: CompactCalendar, weeksAhead: Int) {
}
func didGoToPreviousPage(_ compactCalendar: CompactCalendar, weeksAhead: Int) {
}
}
- 在
viewDidLoad
方法中,将 Compact Calendar 的代理设置为 MyViewController
override func viewDidLoad() {
super.viewDidLoad()
compactCalendar.delegate = self
}
现在你可以自定义 Compact Calendar 并将你的实现添加到代理方法中!
自定义
自定义 Compact Calendar 的各种方法也在此项目的 ViewController.swift 中。你可以克隆此仓库并查看我们提供的示例应用程序,演示了不同的自定义视图和模式的方法。
自定义模式
要自定义模式,你可以在第 7 步的 configure
方法中添加各种参数。
let customSelectedDate = Date().addingTimeInterval(oneDay) // Tomorrow
let customToday = Date().addingTimeInterval(-oneDay) // Yesterday
let customDaysOfTheWeekText = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"]
let customCalendar = Calendar(identifier: .gregorian)
let customTimeZone = TimeZone(identifier: "America/Los_Angeles")
// Tomorrow is initially selected
compactCalendar.configure(selectedDate: customSelectedDate)
// Yesterday is displayed as today
compactCalendar.configure(today: customToday)
// Days of the week bar text is set to the given values
compactCalendar.configure(daysOfTheWeekText: customDaysOfTheWeekText)
// Custom calendar
compactCalendar.configure(calendar: customCalendar)
// Custom TimeZone
if let customTimeZone = customTimeZone {
compactCalendar.configure(timeZone: customTimeZone)
}
// Custom everything
compactCalendar.configure(selectedDate: customSelectedDate, today: customToday, daysOfTheWeekText: customDaysOfTheWeekText, calendar: customCalendar, timeZone: customTimeZone ?? TimeZone.current)
自定义视图
你可以通过更改背景色、前景色或字体来自定义视图的各个组件。我们提供了三个方法 setBackgroundColor
、setForegroundColor
和 setFont
,这些方法接收 CompactCalendar 的 ConfigurableView 和 UIColor 或 UIFont 作为参数,并设置指定可配置视图的属性。
可配置视图
以下是自己所有可配置视图的名称及其含义
以下是如何使用这些方法来自定义视图
// Background Color
compactCalendar.setBackground(forConfigurableView: .monthBar, to: .white)
compactCalendar.setBackground(forConfigurableView: .daysOfTheWeekBar, to: .white)
compactCalendar.setBackground(forConfigurableView: .datesView, to: .white)
// The three lines above are the same as the line below this comment
compactCalendar.setBackground(forConfigurableView: .all, to: .white)
compactCalendar.setBackground(forConfigurableView: .selectedDateView, to: systemRed)
// Foreground Color
compactCalendar.setForeground(forConfigurableView: .monthBar, to: .black)
compactCalendar.setForeground(forConfigurableView: .monthBarButtons, to: systemRed)
compactCalendar.setForeground(forConfigurableView: .daysOfTheWeekBar, to: systemRed)
compactCalendar.setForeground(forConfigurableView: .datesView, to: .black)
compactCalendar.setForeground(forConfigurableView: .selectedDateView, to: .white)
// Font
compactCalendar.setFont(forConfigurableView: .monthBar, to: .systemFont(ofSize: 17))
compactCalendar.setFont(forConfigurableView: .daysOfTheWeekBar, to: .systemFont(ofSize: 13))
compactCalendar.setFont(forConfigurableView: .datesView, to: .systemFont(ofSize: 16))
注意:只有 monthBar
、daysOfTheWeekBar
和 datesView
可以更改它们的字体。
代理方法
CompactCalendarDelegate 协议中有 3 种可用方法。
func compactCalendar(_ compactCalendar: CompactCalendar, didSelectCalendarCellWith date: Date, isAlreadySelected: Bool)
此方法在配置完成后调用一次,并且每次选择的日期发生变化时调用,即用户选择一个单元格,并调用 setDateToToday
和 configure
方法。参数是调用方法 的 Compact Calendar、所选择的日期以及一个布尔值,指示该单元格是否已被选择。
其他方法
func didGoToNextPage(_ compactCalendar: CompactCalendar, weeksAhead: Int)
func didGoToPreviousPage(_ compactCalendar: CompactCalendar, weeksAhead: Int)
当按下跳转到日期下一页的按钮或滑动日历时,compactCalendar
视图通过调用 didGoToNextPage
。参数 weeksAhead
是表示新页比前一页提前多少周的整数值。要注意的一个重要问题是,这不一定总是两周,因为用户可能在调用方法之前滚动多个页面。
相反,当按下上一页按钮或用户滑动到上一页时,将调用 didGoToPreviousPage
,这时因前一页在时间上会是在过去,所以 weeksAhead
将会是负值。
其他方法
你还有一个方法可以使用。
setDateToToday()
此方法将选择并滚动到您设置为今天的日期。您可以使用它来实现一个选择今天日期的按钮,就像我们在示例中做的那样。
贡献
您对我们的 Compact Calendar 有反馈吗?或者想要做出贡献?创建一个问题并分享您的想法和关注点!如果您想做出贡献,请打开一个 PR!我们会尽快查看。请务必遵守 Swift API 指南 并适当地记录您的代码。
测试
在项目中,我们添加了单元测试来确保一切运作正常。在提交PR时,请确保添加任何为您新增功能编写的测试。在提交PR时,CI系统将对您的分支与master分支进行测试,因此在推送代码后确保您的测试通过!
最终笔记
我们很高兴在开源项目中共享CompactCalendar,并欢迎您对我们想要的任何新功能、方法和自定义提出反馈或建议。
在您的应用中使用CompactCalendar?请发给我们链接!我们想看看您是如何使用的!