JBDatePicker 1.2.5

JBDatePicker 1.2.5

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

Tuslareb 维护。



  • 作者:
  • Tuslareb





概述

描述

JBDatePicker是UIView的一个子类,用Swift 4编写,它显示一个月历,用户可以选择一个日期。允许用户在月份之间滑动,并预先选择一个特定的日期。它的外观可以大量自定义。有关更多信息,请参阅使用说明

截图

GIF演示

要求

  • ARC
  • iOS9
  • Swift 4

安装

使用

JBDatePicker可以在你的项目中以两种方式实现

  • Storyboard设置
  • 手动设置

注意:无论你选择哪种设置,JBDatePicker都需要一件事情才能正确工作

  • 你的viewController需要使用JBDatePickerDelegate协议。

除此之外,你需要通过CocoaPods将JBDatePicker集成到项目中。如果你不知道如何做,请遵循这个教程。或者你可以使用Carthage或者将JBDatePicker类手动拖入项目。

Storyboard设置

在你的Storyboard中添加一个UIView,转到身份检查器并选择JBDatePickerView作为新视图的定制类。接下来,打开辅助编辑器并将视图拖控到viewController以创建一个类似这样的出站

@IBOutlet weak var datePicker: JBDatePickerView!

接下来,确保你导入了JBDatePicker并实现了‘JBDatePickerViewDelegate’协议

import JBDatePicker

class ViewController: UIViewController, JBDatePickerViewDelegate {
 
}

别忘了将你的viewController设置为JBDatePicker的委托,例如在viewDidLoad中

override func viewDidLoad() {
    super.viewDidLoad()

    datePicker.delegate = self
}

实现JBDatePickerViewDelegate协议需要一个名为didSelectDay(dayView:)的方法

// MARK: - JBDatePickerViewDelegate implementation

func didSelectDay(_ dayView: JBDatePickerDayView) {
    print("date selected: \(String(describing: dayView.date))")
}

运行应用应该会显示JBDatePicker,并点击一个日期会在控制台打印一条语句。如果不这样做,请检查你是否已设置了委托并调用了updateLayout方法。如果你想自定义JBDatePicker的外观,请继续阅读。

手动设置

也可以不用使用Interface Builder来设置JBDatePicker。以下是一个代码示例

class ViewController: UIViewController, JBDatePickerViewDelegate {

var datePicker: JBDatePickerView!

override func viewDidLoad() {
    super.viewDidLoad()

    let frameForDatePicker = CGRect(x: 0, y: 20, width: view.bounds.width, height: 250)
    datePicker = JBDatePickerView(frame: frameForDatePicker)
    view.addSubview(datePicker)
    datePicker.delegate = self  
}


// MARK: - JBDatePickerViewDelegate

func didSelectDay(_ dayView: JBDatePickerDayView) {
    print("date selected: \(String(describing: dayView.date))")
}

委托功能

除了所需的方法之外,委托还提供了几个可选的方法和属性来实现

/**
Is called when the user swiped (or manually moved) to another month
- parameter monthView: the monthView that is now 'on screen'
*/
func didPresentOtherMonth(_ monthView: JBDatePickerMonthView) {
    print(“month selected: \(monthView.monthDescription)”)
}

/**
Is called to check if any particular date is selectable by the picker
- parameter date: the date to be checked on selectability 
*/
func shouldAllowSelectionOfDay(_ date: Date?) -> Bool {

    //this code example disables selection for dates older then today
    guard let date = date else {return true}
    let comparison = NSCalendar.current.compare(date, to: Date().stripped()!, toGranularity: .day)

    if comparison == .orderedAscending {
        return false
    }
    return true
}

///Sets the day that determines which month is shown on initial load. Defaults to the current date.
var dateToShow: Date { return a Date object}

外观定制

通过实现以下可选属性,可以定制JBDatePicker的几个部分的外观

///Sets the first day of the week. Defaults to the local preference.
var firstWeekDay: JBWeekDay { return .wednesday }

///Determines if a month should also show the dates of the previous and next month. Defaults to true.
var shouldShowMonthOutDates: Bool { return false }

///Determines if the weekday symbols and the month description should follow available localizations. Defaults to false. 
///This means that the weekday symbols and the month description will be in the same language as the device language. 
///If you want it to conform to the localization of your app, return true here. 
///If you return true and your app is not localized, the weekday symbols and the month description will be in the development language.
var shouldLocalize: Bool { return true }

///Determines the height ratio of the weekDaysView compared to the total height. Defaults to 0.1 (10%).
var weekDaysViewHeightRatio: CGFloat { return 0.2 }

/**
Customizes the weekday symbols. Defaults to 'shortStandaloneWeekdaySymbols', but any Calendar API value can be used. It is also possible to return a custom string array. 
- parameter calendar: calendar instance used by the calendar view
*/
func weekdaySymbols(for calendar: Calendar) -> [String]

///Determines the shape that is used to indicate a selected date. Defaults to a circular shape. 
var selectionShape: JBSelectionShape { return .roundedRect }

///Font to be used in dayLabel. Defaults to systemFont of a regular size.
var fontForDayLabel: JBFont { return JBFont(name: "Avenir", size: .medium) }

///Font to be used in the bar which shows the 'mon' to 'sun' labels (weekdaysView). Defaults to systemFont of a regular size.
var fontForWeekDaysViewText: JBFont { return JBFont(name: "Avenir", size: .medium) }

///Color of any date label text that falls within the presented month
var colorForDayLabelInMonth: UIColor { return UIColor of choice }

///Color of any date label text that falls out of the presented month and is part of the next or previous (but not presented) month
var colorForDayLabelOutOfMonth: UIColor { return UIColor of choice }

///Color of any date label text that falls within the presented month but is unavailable because it's selection is now allowed
var colorForUnavaibleDay: UIColor { return UIColor of choice }

///Color of the 'today' date label text
var colorForCurrentDay: UIColor { return UIColor of choice }

///Color of any label text that is selected
var colorForSelelectedDayLabel: UIColor { return UIColor of choice }

///Color of the bar which shows the 'mon' to 'sun' labels. Defaults to green. 
var colorForWeekDaysViewBackground: UIColor { return UIColor of choice }

///Color of the labels in the WeekdaysView bar that say 'mon' to 'sun'. Defaults to white.
var colorForWeekDaysViewText: UIColor { return UIColor of choice }

///Color of the selection circle for dates that aren't today
var colorForSelectionCircleForOtherDate: UIColor { return UIColor of choice }

///Color of the selection circle for today
var colorForSelectionCircleForToday: UIColor { return UIColor of choice }

///Color of the semi selected selection circle (that shows on a long press)
var colorForSemiSelectedSelectionCircle: UIColor { return UIColor of choice }

示例项目

这个仓库中包含了示例项目。要运行示例项目,首先克隆这个仓库,然后在示例目录中运行pod install

作者

Joost van Breukelen

许可证

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