// // DIYViewController.swift // SwiftExample // // Created by dingwenchao on 06/11/2016. // Copyright © 2016 wenchao. All rights reserved. // import Foundation class DIYExampleViewController: UIViewController, FSCalendarDataSource, FSCalendarDelegate, FSCalendarDelegateAppearance { private let gregorian = Calendar(identifier: .gregorian) private let formatter: DateFormatter = { let formatter = DateFormatter() formatter.dateFormat = "yyyy-MM-dd" return formatter }() private weak var calendar: FSCalendar! private weak var eventLabel: UILabel! override func loadView() { let view = UIView(frame: UIScreen.main.bounds) view.backgroundColor = UIColor.groupTableViewBackground self.view = view let height: CGFloat = UIDevice.current.model.hasPrefix("iPad") ? 450 : 300 let calendar = FSCalendar(frame: CGRect(x: 0, y: self.navigationController!.navigationBar.frame.maxY, width: view.frame.size.width, height: height)) calendar.dataSource = self calendar.delegate = self calendar.allowsMultipleSelection = true view.addSubview(calendar) self.calendar = calendar calendar.calendarHeaderView.backgroundColor = UIColor.lightGray.withAlphaComponent(0.1) calendar.calendarWeekdayView.backgroundColor = UIColor.lightGray.withAlphaComponent(0.1) calendar.appearance.eventSelectionColor = UIColor.white calendar.appearance.eventOffset = CGPoint(x: 0, y: -7) calendar.today = nil // Hide the today circle calendar.register(DIYCalendarCell.self, forCellReuseIdentifier: "cell") calendar.clipsToBounds = true // Remove top/bottom line calendar.swipeToChooseGesture.isEnabled = true // Swipe-To-Choose let scopeGesture = UIPanGestureRecognizer(target: calendar, action: #selector(calendar.handleScopeGesture(_:))); calendar.addGestureRecognizer(scopeGesture) let label = UILabel(frame: CGRect(x: 0, y: calendar.frame.maxY + 10, width: self.view.frame.size.width, height: 50)) label.textAlignment = .center label.font = UIFont.preferredFont(forTextStyle: .subheadline) self.view.addSubview(label) self.eventLabel = label let attributedText = NSMutableAttributedString(string: "") let attatchment = NSTextAttachment() attatchment.image = UIImage(named: "icon_cat")! attatchment.bounds = CGRect(x: 0, y: -3, width: attatchment.image!.size.width, height: attatchment.image!.size.height) attributedText.append(NSAttributedString(attachment: attatchment)) attributedText.append(NSAttributedString(string: " Hey Daily Event ")) attributedText.append(NSAttributedString(attachment: attatchment)) self.eventLabel.attributedText = attributedText } override func viewDidLoad() { super.viewDidLoad() self.title = "FSCalendar" // Uncomment this to perform an 'initial-week-scope' // self.calendar.scope = FSCalendarScopeWeek; self.calendar.select(self.gregorian.date(byAdding: .day, value: -1, to: Date()), scrollToDate: false) self.calendar.select(self.gregorian.date(byAdding: .day, value: 0, to: Date()), scrollToDate: false) self.calendar.select(self.gregorian.date(byAdding: .day, value: 1, to: Date()), scrollToDate: false) } deinit { print("\(#function)") } func calendar(_ calendar: FSCalendar, cellFor date: Date, at position: FSCalendarMonthPosition) -> FSCalendarCell { let cell = calendar.dequeueReusableCell(withIdentifier: "cell", for: date, at: position) return cell } func calendar(_ calendar: FSCalendar, willDisplay cell: FSCalendarCell, for date: Date, at position: FSCalendarMonthPosition) { configure(cell: cell, for: date, at: position) } func calendar(_ calendar: FSCalendar, shouldSelect date: Date, at monthPosition: FSCalendarMonthPosition) -> Bool { return monthPosition == .current; } // func minimumDate(for calendar: FSCalendar) -> Date { // return Date() // } // // func maximumDate(for calendar: FSCalendar) -> Date { // let oneYearFromNow = self.gregorian.date(byAdding: .year, value: 1, to: Date(), wrappingComponents: true) // return oneYearFromNow! // } func calendar(_ calendar: FSCalendar, titleFor date: Date) -> String? { if self.gregorian.isDateInToday(date) { return "今" } return nil } func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int { return 2 } func calendar(_ calendar: FSCalendar, boundingRectWillChange bounds: CGRect, animated: Bool) { self.calendar.frame.size.height = bounds.height self.eventLabel.frame.origin.y = calendar.frame.maxY + 10 } func calendar(_ calendar: FSCalendar, didSelect date: Date) { print("did select date \(self.formatter.string(from: date))") self.configureVisibleCells() } func calendar(_ calendar: FSCalendar, didDeselect date: Date) { print("did deselect date \(self.formatter.string(from: date))") self.configureVisibleCells() } func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, eventDefaultColorsFor date: Date) -> [UIColor]? { if self.gregorian.isDateInToday(date) { return [UIColor.orange] } return [appearance.eventDefaultColor] } // MARK: - Private functions private func configureVisibleCells() { calendar.visibleCells().forEach { (cell) in let date = calendar.date(for: cell) let position = calendar.monthPosition(for: cell) self.configure(cell: cell, for: date, at: position) } } private func configure(cell: FSCalendarCell, for date: Date, at position: FSCalendarMonthPosition) { let diyCell = (cell as! DIYCalendarCell) // Custom today circle diyCell.circleImageView.isHidden = !self.gregorian.isDateInToday(date) // Configure selection layer if position == .current || calendar.scope == .week { diyCell.eventIndicator.isHidden = false var selectionType = SelectionType.none if calendar.selectedDates.contains(date) { let previousDate = self.gregorian.date(byAdding: .day, value: -1, to: date)! let nextDate = self.gregorian.date(byAdding: .day, value: 1, to: date)! if calendar.selectedDates.contains(date) { if calendar.selectedDates.contains(previousDate) && calendar.selectedDates.contains(nextDate) { selectionType = .middle } else if calendar.selectedDates.contains(previousDate) && calendar.selectedDates.contains(date) { selectionType = .rightBorder } else if calendar.selectedDates.contains(nextDate) { selectionType = .leftBorder } else { selectionType = .single } } } else { selectionType = .none } if selectionType == .none { diyCell.selectionLayer.isHidden = true return } diyCell.selectionLayer.isHidden = false diyCell.selectionType = selectionType } else if position == .next || position == .previous { diyCell.circleImageView.isHidden = true diyCell.selectionLayer.isHidden = true diyCell.eventIndicator.isHidden = true // Hide default event indicator if self.calendar.selectedDates.contains(date) { diyCell.titleLabel!.textColor = self.calendar.appearance.titlePlaceholderColor // Prevent placeholders from changing text color } } } }