CalendarKit Documentation

repository·master·Indexed 25 days ago

https://github.com/richardtop/calendarkit

A modular Swift UI library for iOS 11.0+ and Mac Catalyst 10.15+ that provides a calendar interface similar to the Apple Calendar app. It includes components for displaying events via EventDataSource, handling user interaction through DayViewDelegate, and customizing appearance with CalendarStyle.

Tokens
915
Snippets
4
Records
5
Agent score
33%

What's inside CalendarKit

  1. Display events by implementing EventDataSource

    master

    To show events in your calendar, you must subclass DayViewController and implement the EventDataSource protocol. The eventsForDate(_:) method must return an array of objects conforming to the EventDescriptor protocol. You can use the provided Event class as your model.

    // Return an array of EventDescriptors for particular date
    override func eventsForDate(_ date: Date) -> [EventDescriptor] {
      var models = myAppEventStore.getEventsForDate(date) // Get events (models) from the storage / API
    
      var events = [Event]()
    
      for model in models {
          // Create new EventView
          let event = Event()
          // Specify DateInterval
          event.dateInterval = DateInterval(start: model.startDate, end: model.endDate)
          // Add info: event title, subtitle, location to the array of Strings
          var info = [model.title, model.location]
          info.append("\(datePeriod.beginning!.format(with: "HH:mm")) - \(datePeriod.end!.format(with: "HH:mm"))")
          // Set "text" value of event by formatting all the information needed for display
          event.text = info.reduce("", {$0 + $1 + "\n"})
          events.append(event)
      }
      return events
    }
  2. Customize the calendar appearance with CalendarStyle

    master

    CalendarKit supports Dark Mode by default and allows for custom styling. To apply a custom style:

    1. Create a new CalendarStyle object.
    2. Update its properties (e.g., backgroundColor).
    3. Call the updateStyle(_:) method on your dayView instance.
    let style = CalendarStyle()
    style.backgroundColor = UIColor.black
    dayView.updateStyle(style)
  3. Install CalendarKit via Swift Package Manager

    master

    To add CalendarKit to your Xcode project, follow these steps:

    1. In Xcode, navigate to FileSwift PackagesAdd Package Dependency...
    2. Paste the repository URL: https://github.com/richardtop/CalendarKit.git
    3. For Rules, select Version (Up to Next Major).
    4. Click Finish.
    https://github.com/richardtop/CalendarKit.git
  4. Handle user interaction with DayViewDelegate

    master

    To respond to user inputs such as selecting or long-pressing an event, override the corresponding methods in DayViewDelegate.

    override func dayViewDidSelectEventView(_ eventView: EventView) {
      print("Event has been selected: \(eventview.data)")
    }
    
    override func dayViewDidLongPressEventView(_ eventView: EventView) {
      print("Event has been longPressed: \(eventView.data)")
    }