测试已测试 | ✓ |
Lang语言 | SwiftSwift |
许可证 | MIT |
发布最后发布 | 2016年2月 |
SPM支持 SPM | ✗ |
由 Nate Armstrong 和 Nate Armstrong 维护。
要运行示例项目,先克隆仓库,然后在 CalendarViewDemo 目录下运行 pod install
。
CalendarView 通过 Carthage 和 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中:
pod "CalendarView"
在代码中使用 CalendarView
类
let calendar = CalendarView(frame: CGRectMake(0, 0, CGRectGetWidth(view.frame), 320))
view.addSubview(calendar)
或作为输出(支持自动布局)
@IBOutlet weak var calendar: CalendarView!
默认情况下,选中日期为当前日期。您可以使用 selectDate(date: Moment)
方法选择任何日期。
let date: NSDate = MY_NSDATE
calendar.selectDate(moment(date))
CalendarView
的代理会收到两个事件的通知
calendarDidSelectDate(date: Moment) // called when user taps a date
calendarDidPageToDate(date: Moment) // called when users swipes month
CalendarView
类使用 SwiftMoment 进行日期操作。
extension ViewController: CalendarViewDelegate {
func calendarDidSelectDate(date: Moment) {
title = date.format(dateFormat: "MMMM d, yyyy")
}
func calendarDidPageToDate(date: Moment) {
title = date.format(dateFormat: "MMMM d, yyyy")
}
}
目标是使日历尽可能可定制,同时又不使其过于复杂和臃肿。
您可以通过设置 CalendarView
的某些类属性来自定义日历的外观。
import UIKit
import CalendarView
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Calendar appearance
CalendarView.daySelectedBackgroundColor = UIColor.secondaryColor()
CalendarView.daySelectedTextColor = UIColor.whiteColor()
CalendarView.todayBackgroundColor = UIColor(white: 0.0, alpha: 0.3)
CalendarView.todayTextColor = UIColor.whiteColor()
CalendarView.otherMonthBackgroundColor = UIColor.clearColor()
CalendarView.otherMonthTextColor = UIColor(white: 1.0, alpha: 0.3)
CalendarView.dayTextColor = UIColor(white: 1.0, alpha: 0.6)
CalendarView.dayBackgroundColor = UIColor.clearColor()
CalendarView.weekLabelTextColor = UIColor(white: 1.0, alpha: 0.3)
return true
}
}
默认情况下,用户拖动到不同月份时,系统会自动选择该月的第一天。您可以通过修改 CalendarView
实例的 selectedDayOnPaged
属性来自定义此行为。
public var selectedDayOnPaged: Int? = 1
如果设置为 nil
,则拖动时不会自动选择任何一天。
Nate Armstrong,[email protected]
CalendarView 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。