DTCalendar 1.0.5

DTCalendar 1.0.5

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布上次发布2017年6月
SwiftSwift版本3.0
SPM支持SPM

Dan JiangDan Jiang维护。



DTCalendar

简介

简单的日历控件。

Demo

全功能控制。

Full controls

隐藏日期控件。

Hide day controls

隐藏月份控件。

Hide month controls

隐藏所有控件。

Hide both controls

高亮为矩形填充。

Rect fill

高亮为矩形轮廓。

Rect stroke

高亮为椭圆形填充。

Oval fill

高亮为椭圆形轮廓。

Oval stroke

使用表情符号自定义徽章。

Emoji badge

安装

需求

iOS 8.4+

使用方法

导入

import DTCalendar

使用

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

自定义

控件

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)

高亮

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

主题

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

徽章

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