An easy-to-use and highly customizable month calendar.
This frameworks provides a month calendar picker with both UIKit and SwiftUI variants.
Y—CalendarPicker is licensed under the Apache 2.0 license.
Documentation is automatically generated from source code comments and rendered as a static website hosted via GitHub Pages at: https://yml-org.github.io/ycalendarpicker-ios/
CalendarPicker
(UIKit)CalendarPicker
is a subclass of UIControl
with an api similar to UIDatePicker
.
CalendarView
(SwiftUI)CalendarView
is a struct that conforms to the SwiftUI View
protocol.
Both CalendarPicker
and CalendarView
can be initialized with the same five parameters (CalendarPicker
uses CalendarView
internally):
init(
firstWeekday: Int? = nil,
appearance: Appearance = .default,
minimumDate: Date? = nil,
maximumDate: Date? = nil,
locale: Locale? = nil
)
The standard initializer lets you specify the first day of the week, appearance, optional minimum and maximum dates, and the locale, although it provides sensible defaults for all of these.
CalendarPicker
has an additional initializer:
init?(coder: NSCoder)
For use in Interface Builder or Storyboards (although we recommend that you build your UI in code).
A calendar picker created this way begins with the default appearance, but you can customize it at runtime by updating its appearance
property.
CalendarPicker
and CalendarView
both have an appearance
property of type Appearance
.
Appearance
lets you customize the picker's appearance. You have full control over the colors, typographies, and images used. The default appearance is dark mode compatible and WCAG 2.0 AA compliant for color contrast.
/// Appearance for CalendarPicker that contains typography and color properties
public struct Appearance {
/// Appearance for days within current month
public var normalDayAppearance: Day
/// Appearance for days outside current month
public var grayedDayAppearance: Day
/// Appearance for today
public var todayAppearance: Day
/// Appearance for selected day
public var selectedDayAppearance: Day
/// Appearance for disabled day
public var disabledDayAppearance: Day
/// Appearance for booked day
public var bookedDayAppearance: Day
/// Foreground color and typography for weekdays
public var weekdayStyle: (textColor: UIColor, typography: Typography)
/// Image for previous month button
///
/// Images with template rendering mode will be tinted to `monthForegroundColor`.
public var previousImage: UIImage?
/// Image for next month button
///
/// Images with template rendering mode will be tinted to `monthForegroundColor`.
public var nextImage: UIImage?
/// Foreground color and typography for month (and year)
public var monthStyle: (textColor: UIColor, typography: Typography)
/// Background color for calendar view
public var backgroundColor: UIColor
}
The calendar has six different appearances for drawing individual days:
minimumDate
or after maximumDate
. These days are not selectable.The appearance of each of these types of days can be customized using the Day
structure.
/// Appearance for Date
public struct Day {
/// Typography for day view
public var typography: Typography
/// Foreground color for day view
public var foregroundColor: UIColor
/// Background color for day view
public var backgroundColor: UIColor
/// Border color for day view
public var borderColor: UIColor
/// Border width for day view
public var borderWidth: CGFloat
}
How to import?
import YCalendarPicker
Create a calendar picker
// Create calendar picker with default values
let calendarPicker = CalendarPicker()
// add calendar picker to any view
view.addSubview(calendarPicker)
Customize and then update appearance
// Create a calendar picker with the weekday text color set to green
var calendarPicker = CalendarPicker(
appearance: CalendarPicker.Appearance(weekdayStyle: (textColor: .green, typography: .weekday)
)
// Change the weekday text color to red
calendarPicker.appearance.weekdayStyle.textColor = .red
Update Calendar properties
// set minimum date to yesterday and maximum date to tomorrow
calendarPicker.minimumDate = Date().previousDate()
calendarPicker.maximumDate = Date().nextDate()
// select today's date
calendarPicker.date = Date()
Receive change notifications
To be notified when the date changes, simply use the target-action mechanism exactly as you would for UIDatePicker
.
// Add target with action
calendarPicker.addTarget(self, action: #selector(onDateChange), for: .valueChanged)
If you wish to know when the user has switched months (via the previous and next buttons), you can use the picker's delegate
property and conform to the CalendarPickerDelegate
protocol.
// Create calendar picker
let calendarPicker = CalendarPicker()
// set the delegate to be notified when the month changes
calendarPicker.delegate = self
// This will notify when the user presses the next/previous buttons
extension DemoViewController: CalendarPickerDelegate {
func calendarPicker(_ calendarPicker: CalendarPicker, didChangeMonthTo date: Date) {
print("New month: \(date)")
}
}
Our calendar picker also supports Swift UI!
How to import?
import YCalendarPicker
Create a calendar view
CalendarView
conforms to SwiftUI's View
protocol so we can directly integrate CalendarView
with any SwiftUI view.
var body: some View {
CalendarView()
}
Customize and then update appearance
struct CustomCalendar {
@State var calendar: CalendarView = {
// Create a calendar picker with the weekday text color set to green
var calendar = CalendarView()
calendar.appearance.weekdayStyle.textColor = .green
return calendar
}()
}
extension CustomCalendar: View {
public var body: some View {
VStack {
calendar
Button("Go Red") {
// Change the weekday text color to red
calendar.appearance.weekdayStyle.textColor = .red
}
}
}
}
Update Calendar properties
struct CustomCalendar {
@State var calendar = CalendarView()
}
extension CustomCalendar: View {
var body: some View {
VStack {
calendar
Button("Set Min/Max") {
// set minimum date to yesterday and maximum date to tomorrow
calendar.minimumDate = Date().previousDate()
calendar.maximumDate = Date().nextDate()
}
Button("Select Today") {
// select today's date
calendar.date = Date()
}
}
}
}
Receive change notifications
To be notified when the user selects a date or changes the month, you can use the delegate
property and conform to the CalendarViewDelegate
protocol.
extension DemoView: CalendarViewDelegate {
// Date was selected
func calendarViewDidSelectDate(_ date: Date?) {
if let date {
print("Selected: \(date)")
} else {
print("Selection cleared")
}
}
// Month was changed
func calendarViewDidChangeMonth(to date: Date) {
print("New month: \(date)")
}
}
Y—CalendarPicker depends upon our Y—CoreUI and Y—MatterType frameworks (both also open source and Apache 2.0 licensed).
You can add Y—CalendarPicker to an Xcode project by adding it as a package dependency.
brew install swiftlint
sudo gem install jazzy
Clone the repo and open Package.swift
in Xcode.
We utilize semantic versioning.
{major}.{minor}.{patch}
e.g.
1.0.5
We utilize a simplified branching strategy for our frameworks.
main
main
main
as they are completed and approved.main
gets tagged with an updated version # for each releasefeature/{ticket-number}-{short-description}
bugfix/{ticket-number}-{short-description}
e.g.
feature/CM-44-button
bugfix/CM-236-textview-color
Prior to submitting a pull request you should:
swiftlint
from the command line and confirm that there are no violations.jazzy
from the command line and confirm that you have 100% documentation coverage.git rebase -i HEAD~{commit-count}
to squash your last {commit-count} commits together into functional chunks.main
) has been updated since you created your branch, use git rebase main
to rebase your branch.
When submitting a pull request:
When merging a pull request:
1.0.5
)You can generate your own local set of documentation directly from the source code using the following command from Terminal:
jazzy
This generates a set of documentation under /docs
. The default configuration is set in the default config file .jazzy.yaml
file.
To view additional documentation options type:
jazzy --help
A GitHub Action automatically runs each time a commit is pushed to main
that runs Jazzy to generate the documentation for our GitHub page at: https://yml-org.github.io/ycalendarpicker-ios/
link |
Stars: 7 |
Last commit: 3 days ago |
Initial Release
Full Changelog: https://github.com/yml-org/ycalendarpicker-ios/commits/1.0.0
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics