Latest 1.0.5
Homepage https://github.com/danjiang/DTCalendar
License MIT
Platforms ios 8.4, requires ARC
Authors

Swift
Platform
CocoaPods
Carthage compatible

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+

CocoaPods

To install DTCalendar add a dependency to your Podfile:

pod "DTCalendar"

Carthage

To install DTCalendar add a dependency to your Cartfile:

github "danjiang/DTCalendar"
carthage update --platform ios

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()
    }
  }

}

Latest podspec

{
    "name": "DTCalendar",
    "version": "1.0.5",
    "summary": "Simple calendar control",
    "homepage": "https://github.com/danjiang/DTCalendar",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Dan Jiang": "[email protected]"
    },
    "social_media_url": "http://twitter.com/dtstudio",
    "platforms": {
        "ios": "8.4"
    },
    "requires_arc": true,
    "source": {
        "git": "https://github.com/danjiang/DTCalendar.git",
        "tag": "1.0.5",
        "submodules": true
    },
    "source_files": "Sources/**/*.{h,swift}",
    "resources": "Sources/*.bundle",
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This