DTCalendarView 0.10.0

DTCalendarView 0.10.0

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最后发布2017年10月
SwiftSwift版本3.0
SPM支持SPM

Tim LeMaster维护。



  • 作者
  • Tim LeMaster

DTCalendarView

DTCalendarView是一个用于显示垂直滚动日历的库。它支持单值和范围选择以及所选日期的拖动。大多数元素的字体和颜色可以自定义。

alt text

DTCalendarView设计得简单易用,同时功能强大,因为它将代码放在处理日期选择时的高度控制权。

要使用它,只需将其添加到视图中,无论是在代码中还是在Storyboard中。日历会尽可能填充视图。

@IBOutlet private var calendarView: DTCalendarView! {
        didSet {
            calendarView.delegate = self

            calendarView.displayEndDate = Date(timeIntervalSinceNow: 60 * 60 * 24 * 30 * 12 * 2)
            calendarView.previewDaysInPreviousAndMonth = true
            calendarView.paginateMonths = true
        }
    }

实现代理以控制日历观看日期选择、拖动等的工作方式。日历视图没有假设当日期被点击时应该发生什么。也就是说,它不会自动选择。这允许你对选择行为有完全控制。

extension ViewController: DTCalendarViewDelegate {

    func calendarView(_ calendarView: DTCalendarView, dragFromDate fromDate: Date, toDate: Date) {

        if let startDate = calendarView.selectionStartDate,
            fromDate == startDate {

            if let endDate = calendarView.selectionEndDate {
                if toDate < endDate {
                    calendarView.selectionStartDate = toDate
                }
            } else {
                calendarView.selectionStartDate = toDate
                }

        } else if let endDate = calendarView.selectionEndDate,
            fromDate == endDate {

            if let startDate = calendarView.selectionStartDate {
                if toDate > startDate {
                    calendarView.selectionEndDate = toDate
                    }
            } else {
                calendarView.selectionEndDate = toDate
            }
        }
    }


    func calendarView(_ calendarView: DTCalendarView, viewForMonth month: Date) -> UIView {

        let label = UILabel()
        label.text = monthYearFormatter.string(from: month)
        label.textColor = UIColor.black
        label.textAlignment = .center
        label.backgroundColor = UIColor.white

        return label
    }

    func calendarView(_ calendarView: DTCalendarView, didSelectDate date: Date) {
    }

    ...

日历视图的UI也是高度可定制的。代理提供顶部月份标题的视图,允许其外观任意。UI的其他部分由显示属性控制。这是一个结构体,指示视图在给定状态时的外观(类似于UIButton.setTitle(String? for: UIControlState))。

if let font = R.font.textaBold(size: 15) {
  calendarView.weekdayDisplayAttributes = DisplayAttributes(font: font,
                                                            textColor: .white,
                                                            backgroundColor: .clear,
                                                            textAlignment: .center)

  calendarView.setDisplayAttributes(DisplayAttributes(font: font,
                                                      textColor: .white,
                                                      backgroundColor: .clear,
                                                      textAlignment: .center), forState: .normal)

  calendarView.setDisplayAttributes(DisplayAttributes(font: font,
                                                      textColor: UIColor.white.withAlphaComponent(0.5),
                                                      backgroundColor: .clear,
                                                      textAlignment: .center), forState: .preview)

  calendarView.setDisplayAttributes(DisplayAttributes(font: font,
                                                      textColor: R.color.app.primaryAction(),
                                                      backgroundColor: .white,
                                                      textAlignment: .center), forState: .selected)

  calendarView.setDisplayAttributes(DisplayAttributes(font: font,
                                                      textColor: .white,
                                                      backgroundColor: UIColor.white.withAlphaComponent(0.5),
                                                      textAlignment: .center), forState: .highlighted)
  }

示例

要运行示例项目,请克隆仓库,然后首先从“Example”目录中运行pod install

要求

  • Swift 3
  • Xcode 8

安装

DTCalendarView通过CocoaPods可用。要安装
它,只需要将以下行添加到你的Podfile中。

pod "DTCalendarView"

贡献

我们欢迎拉取请求。

作者

[email protected]

许可证

DTCalendarView在MIT许可证下可用。有关更多信息,请参阅LICENSE文件。