JTAppleCalendar Documentation

repository·master·Indexed 27 days ago

https://github.com/patchthecode/jtapplecalendar

A highly customizable iOS calendar library providing complete control over date cells, headers, and layouts. Key features include range selection, boundary dates, week/month mode switching, custom cell design, and support for both horizontal and vertical orientations. Includes guides for implementing JTAppleCalendarViewDataSource and JTAppleCalendarViewDelegate, managing event data with Date-keyed dictionaries, and migrating to Version 8.0.0.

Tokens
10.6K
Snippets
23
Records
39
Agent score
89%

What's inside JTAppleCalendar

  1. Overview of JTAppleCalendar features

    master

    JTAppleCalendar is a highly customizable calendar library for iOS that allows developers to design their own date cells and calendar views. Key features include:

    • Range selection: Select dates within a range with custom designs.
    • Boundary dates: Limit the available date range in the calendar.
    • Week/month mode: Switch between month view and week view (showing 1, 2, 3, or 6 rows of weekdays).
    • Custom cells: Complete control over the appearance and functionality of day-cells.
    • Custom calendar view: Ability to build custom calendar layouts.
    • First Day of week: Configure which day starts the week.
    • Orientation: Supports both horizontal and vertical modes.
    • Month headers: Add headers in varying sizes and styles.
    • Navigation: Ability to scroll to any specific month using a date.
  2. Get started with JTAppleCalendar

    master

    JTAppleCalendar is a highly customizable calendar library for iOS that provides a layout (7 columns and 1-6 rows) without enforcing a specific visual design.

    Key features include:

    • Total customization: Full control over the visual design of cells and the calendar layout.
    • Scrolling modes: Support for both horizontal and vertical scrolling.
    • Selection modes: Supports regular date selection and range selection.
    • Flexible views: Switch between week and month modes (1 to 6 rows).
    • Customization: Ability to set a custom first day of the week and add headers of varying sizes.

    Note on Documentation Version: The current 'Get Started' guide is based on version 7.1.7 and may be out of date. If you are using version 8.0.0 or later, please refer to the Version 8.0.0 migration guide.

  3. Style inDates, outDates, and monthDates using CellState

    master

    To distinguish between dates belonging to the current month and those belonging to adjacent months (inDates/outDates), use the cellState.dateBelongsTo property within your cell configuration logic.

    Available dateBelongsTo values:

    • thisMonth
    • previousMonthWithinBoundary
    • previousMonthOutsideBoundary
    • followingMonthWithinBoundary
    • followingMonthOutsideBoundary
    func handleCellTextColor(cell: DateCell, cellState: CellState) {
       if cellState.dateBelongsTo == .thisMonth {
          cell.dateLabel.textColor = UIColor.black
       } else {
          cell.dateLabel.textColor = UIColor.gray
       }
    }
  4. Create and configure WeekCountCell

    master

    Create a custom UICollectionViewCell class to manage the display of week numbers.

    1. Define a class inheriting from UICollectionViewCell with an @IBOutlet for a UILabel.
    2. In Storyboard, set the class of the cell within your UICollectionView to WeekCountCell.
    3. Connect the countLabel outlet from the Storyboard to your code.
    import UIKit
    
    class WeekCountCell: UICollectionViewCell {
        @IBOutlet var countLabel: UILabel!
    }
  5. Implement scrolling headers using JTAppleCollectionReusableView

    master

    Unlike static headers created outside the calendar (e.g., using a UIStackView), 'Inside Headers' are generated by the calendar and scroll with the view. To implement them, follow these steps:

    1. Create a Reusable View Class: Subclass JTAppleCollectionReusableView to define your header.
    2. Configure Storyboard: Add a section header to your CollectionView, set its reusable identifier (e.g., "DateHeader"), and assign your custom class.
    3. Implement Delegate Methods: Use headerViewForDateRange to dequeue the view and populate it with data, and calendarSizeForMonths to define the header height.
    import UIKit
    import JTAppleCalendar
    
    // 1. Define the custom header class
    class DateHeader: JTAppleCollectionReusableView {
    @IBOutlet var monthTitle: UILabel!
    }
    
    // 2. Implement the delegate methods in your ViewController
    func calendar(\_ calendar: JTAppleCalendarView, headerViewForDateRange range: (start: Date, end: Date), at indexPath: IndexPath) -> JTAppleCollectionReusableView {
        let formatter = DateFormatter() 
        formatter.dateFormat = "MMM"
        let header = calendar.dequeueReusableJTAppleSupplementaryView(withReuseIdentifier: "DateHeader", for: indexPath) as! DateHeader
        header.monthTitle.text = formatter.string(from: range.start)
        return header
    }
    
    func calendarSizeForMonths(\_ calendar: JTAppleCalendarView?) -> MonthSize? {
        return MonthSize(defaultSize: 50)
    }
  6. Configure the week number UICollectionView

    master

    Set up the UICollectionView in your ViewController to manage the week number cells.

    1. Create an @IBOutlet for the UICollectionView in your ViewController subclass.
    2. Connect the outlet in Storyboard.
    3. Set the dataSource and delegate of the UICollectionView to your ViewController.
    class ViewController: UIViewController {
        @IBOutlet var calendarView: JTAppleCalendarView!
        @IBOutlet var weekCount: UICollectionView!
    
        override func viewDidLoad() {
            // Connect weekCount to the UICollectionView in Storyboard
            // Set weekCount.dataSource and weekCount.delegate to self
        }
    }
  7. Configure calendar parameters for switching between month and week views

    master

    The JTAppleCalendar library does not have a built-in concept of 'week' or 'month' views. Instead, you implement this by switching the numberOfRows in your ConfigurationParameters.

    When switching to a 1-row view (week view), you must adjust how in-dates and out-dates are generated to prevent dates from appearing to repeat. A proper configuration for a 1-row calendar uses generateInDates: .forFirstMonthOnly and generateOutDates: .off with hasStrictBoundaries: false.

    func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy MM dd"
    
        let startDate = formatter.date(from: "2018 01 01")!
        let endDate = Date()
    
        if numberOfRows == 6 {
            return ConfigurationParameters(startDate: startDate, endDate: endDate, numberOfRows: numberOfRows)
        } else {
            return ConfigurationParameters(startDate: startDate,
                                           endDate: endDate,
                                           numberOfRows: numberOfRows,
                                           generateInDates: .forFirstMonthOnly,
                                           generateOutDates: .off,
                                           hasStrictBoundaries: false)
        }
    }
  8. Set up JTAppleCalendar using Storyboard

    master

    To use JTAppleCalendar with Storyboard, follow these steps:

    1. Configure UICollectionView: Drag a UICollectionView onto your screen. Change its class to JTAppleCalendarView and its module to JTAppleCalendar.
    2. Set Constraints: Set both height and width constraints. The library requires these to determine cell size.
    3. Adjust Spacing: Set both minimumCellSpacing and minimumLineSpacing to zero unless your design requires otherwise.
    4. Connect Delegates: In Storyboard, set the calendar's ibCalendarDataSource and ibCalendarDelegate to your ViewController subclass.
    5. Configure Cell: Create a custom cell class inheriting from JTAppleCell, set its reusableIdentifier to dateCell, and connect your UI elements via @IBOutlet.
    import JTAppleCalendar
    import UIKit
    
    class DateCell: JTAppleCell {
        @IBOutlet var dateLabel: UILabel!
    }