DTCalendar 1.0.5

DTCalendar 1.0.5

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Jun 2017
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by Dan Jiang, Dan Jiang.



DTCalendar

Introduction

Simple calendar control.

Demo

Full controls.

Full controls

Hide day controls.

Hide day controls

Hide month controls.

Hide month controls

Hide both controls.

Hide both controls

Highlight is rect fill.

Rect fill

Highlight is rect stroke.

Rect stroke

Highlight is oval fill.

Oval fill

Highlight is oval stroke.

Oval stroke

Customize badge with emoji.

Emoji badge

Installation

Requirement

iOS 8.4+

Usage

Import

import DTCalendar

Use

class ViewController: UIViewController {

  @IBOutlet weak var label: UILabel!
  
  fileprivate let formatter = DateFormatter()
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    formatter.timeStyle = .none
    formatter.dateStyle = .full
    label.text = formatter.string(from: Date())
    
    let calendar = DTCalendar()
    calendar.dataSource = self
    calendar.delegate = self
    
    view.addSubview(calendar)
    
    calendar.translatesAutoresizingMaskIntoConstraints = false
    
    let top = NSLayoutConstraint(item: calendar, attribute: .top, relatedBy: .equal, toItem: topLayoutGuide, attribute: .bottom, multiplier: 1, constant: 0)
    let leading = NSLayoutConstraint(item: calendar, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 0)
    let trailing = NSLayoutConstraint(item: calendar, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 0)
    
    view.addConstraints([top, leading, trailing])
  }

}

extension ViewController: DTCalendarDataSource {
  
  func calendar(_ calendar: DTCalendar, badgeForDate date: Date) -> DTBadge? {
    let dateComponents = Calendar.current.dateComponents([.weekday], from: date)
    if let weekday = dateComponents.weekday {
      if weekday == 2 || weekday == 4 || weekday == 6 {
        return DTCircleBadge()
      }
    }
    return nil
  }
  
}

extension ViewController: DTCalendarDelegate {
  
  func calendar(_ calendar: DTCalendar, didSelectedDate date: Date, isDifferentMonth: Bool) {
    label.text = formatter.string(from: date)
    if isDifferentMonth {
      calendar.reloadBadges()
    }
  }
  
}

Customize

Controls

DTCalendar.isControlsOnTop = false // Put controls on bottom
DTCalendar.isMonthControlsHidden = true // Hide month controls
DTCalendar.isDayControlsHidden = true // Hide day controls

// Write your own controls. Control calendar through following public methods:

public func goPreviousMonth()
public func goNextMonth()
public func goYesterday()
public func goTomorrow()
public func goToday()
public func goDate(_ date: Date)

Highlight

DTCalendar.highlight = .rectFill // Rect Fill is default highlight
DTCalendar.highlight = .rectStroke
DTCalendar.highlight = .ovalFill
DTCalendar.highlight = .ovalStroke

Theme

struct MyTheme: DTCalendarTheme {
  
  var backgroundColor: UIColor {
    return UIColor(white: 0.26, alpha: 1)
  }
  
  var controlsColor: UIColor {
    return UIColor.white
  }

  var weekdayLabelColor: UIColor {
    return UIColor.white
  }
  
  var dayHighlightColor: UIColor {
    return UIColor(white: 0.86, alpha: 1)
  }
  
  var todayHighlightColor: UIColor {
    return UIColor(red: 0.95, green: 0.33, blue: 0.17, alpha: 1)
  }
  
  var selectedHighlightColor: UIColor {
    return UIColor(white: 0.57, alpha: 1)
  }
  
  var dayLabelColor: UIColor {
    return UIColor(white: 0.26, alpha: 1)
  }
  
  var todayLabelColor: UIColor {
    return UIColor.white
  }

  var selectedLabelColor: UIColor {
    return UIColor.white
  }

  var dayBadgeColor: UIColor {
    return UIColor(white: 0.26, alpha: 1)
  }
  
  var todayBadgeColor: UIColor {
    return UIColor.white
  }
  
  var selectedBadgeColor: UIColor {
    return UIColor.white
  }
  
}

DTCalendar.theme = MyTheme()

Badge

class EmojiBadge: DTBadge {
  
  let label = UILabel()
  
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

  override init(frame: CGRect) {
    super.init(frame: frame)
    
    addSubview(label)
    
    label.translatesAutoresizingMaskIntoConstraints = false
    
    let top = NSLayoutConstraint(item: label, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 0)
    let bottom = NSLayoutConstraint(item: label, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 0)
    let leading = NSLayoutConstraint(item: label, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1, constant: 0)
    let trailing = NSLayoutConstraint(item: label, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1, constant: 0)
    
    addConstraints([top, bottom, leading, trailing])
  }
  
  override func didChangeStyle(_ style: Style) {
    switch style {
    case .normal:
      label.font = UIFont.systemFont(ofSize: 8)
    case .today:
      label.font = UIFont.systemFont(ofSize: 8)
    case .selected:
      label.font = UIFont.systemFont(ofSize: 16)
    }
  }

}

extension ViewController: DTCalendarDataSource {
  
  func calendar(_ calendar: DTCalendar, badgeForDate date: Date) -> DTBadge? {
    let dateComponents = Calendar.current.dateComponents([.weekday], from: date)
    if let weekday = dateComponents.weekday {
      if weekday == 2 || weekday == 4 || weekday == 6 {
        let badge = EmojiBadge()
        badge.label.text = "⭐️"
        return badge
      }
    }
    return nil
  }
  
}

extension ViewController: DTCalendarDelegate {
  
  func calendar(_ calendar: DTCalendar, didSelectedDate date: Date, isDifferentMonth: Bool) {
    label.text = formatter.string(from: date)
    if isDifferentMonth {
      calendar.reloadBadges()
    }
  }
  
}