-
Notifications
You must be signed in to change notification settings - Fork 0
/
CalendarView.swift
359 lines (314 loc) · 13.2 KB
/
CalendarView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
//
// CalendarView.swift
// FSCalendarBootstrap
//
// Created by Daniel on 2017-09-07.
// Copyright © 2017 danielbogomazov. All rights reserved.
//
import UIKit
// TODO : Get rid of the CalendarTextColor Protocol things and add { get, set } to the color vars in the class itself
@objc protocol CalendarDelegate {
@objc optional func didSelect(date: Date)
@objc optional func maximumDate() -> Date
@objc optional func minimumDate() -> Date
}
class CalendarView: UIView, FSCalendarDataSource {
/* Views */
var view: UIView!
fileprivate var topPadding: UIView!
fileprivate var calendar: FSCalendar!
fileprivate var yearTextField: UITextField!
/* FSCalendar Variables */
var headerDateFormat: String = "MMMM" {
didSet {
self.calendar.appearance.headerDateFormat = headerDateFormat
}
}
var scrollDirection: FSCalendarScrollDirection = .horizontal {
didSet {
self.calendar.scrollDirection = scrollDirection
}
}
var allowsMultipleSelection: Bool = false {
didSet {
self.calendar.allowsMultipleSelection = allowsMultipleSelection
}
}
var firstWeekday: UInt = 1 {
didSet {
self.calendar.firstWeekday = firstWeekday
}
}
var weekdayTextColor: UIColor = UIColor.black {
didSet {
self.calendar.appearance.weekdayTextColor = weekdayTextColor
}
}
var headerTitleColor: UIColor = UIColor.white {
didSet {
self.calendar.appearance.headerTitleColor = headerTitleColor
}
}
var eventDefaultColor: UIColor = UIColor.clear {
didSet {
self.calendar.appearance.eventDefaultColor = eventDefaultColor
}
}
var eventSelectionColor: UIColor = UIColor.clear {
didSet {
self.calendar.appearance.eventSelectionColor = eventSelectionColor
}
}
var selectionColor: UIColor = UIColor(red: 220/255, green: 95/255, blue: 70/255, alpha: 1.0) {
didSet {
self.calendar.appearance.selectionColor = selectionColor
}
}
var todayColor: UIColor = UIColor.white {
didSet {
self.calendar.appearance.todayColor = todayColor
}
}
var todaySelectionColor: UIColor = UIColor(red: 220/255, green: 95/255, blue: 70/255, alpha: 1.0) {
didSet {
self.calendar.appearance.todaySelectionColor = todaySelectionColor
}
}
var yearTextColor: UIColor = UIColor.white.withAlphaComponent(0.5) {
didSet {
self.yearTextField.textColor = yearTextColor
}
}
var titleTodayColor: UIColor = UIColor(red: 220/255, green: 95/255, blue: 70/255, alpha: 1.0) {
didSet {
self.calendar.appearance.titleTodayColor = titleTodayColor
}
}
var borderRadius: CGFloat = 1 {
didSet {
self.calendar.appearance.borderRadius = borderRadius
}
}
/* Additional Calendar Variables */
var calendarColor: UIColor = UIColor.white {
didSet {
self.calendar.backgroundColor = calendarColor
}
}
var headerColor: UIColor = UIColor(red: 220/255, green: 95/255, blue: 70/255, alpha: 1.0) {
didSet {
self.calendar.calendarHeaderView.backgroundColor = headerColor
self.view.backgroundColor = headerColor
}
}
/* Functionalities/Parameters */
fileprivate var minimumDate: Date!
fileprivate var maximumDate: Date!
fileprivate var dateFormatter: DateFormatter!
/* Misc. */
var delegate: CalendarDelegate? {
didSet {
calendar.reloadData()
selectDefaultDate()
}
}
/* Constants */
fileprivate let kMaxYearCharactersCount: Int = 4
init(frame: CGRect, minimumDate: Date!, maximumDate: Date!) {
super.init(frame: frame)
/* Initialize Variables */
self.minimumDate = minimumDate
self.maximumDate = maximumDate
self.dateFormatter = DateFormatter()
/* Build Calendar View */
let viewWidth = frame.width
let viewHeight = frame.height
let topPaddingHeight: CGFloat = 15.0
let yearLabelHeight: CGFloat = 20.0
let calendarHeight = viewHeight - topPaddingHeight
self.view = UIView(frame: frame)
self.calendar = FSCalendar(frame: CGRect(x: 0, y: topPaddingHeight, width: viewWidth, height: calendarHeight))
self.calendar.delegate = self
self.calendar.dataSource = self
self.calendar.appearance.headerMinimumDissolvedAlpha = 0
self.calendar.clipsToBounds = true
selectDefaultDate()
self.view.addSubview(self.calendar)
self.topPadding = UIView(frame: CGRect(x: 0, y: 0, width: viewWidth, height: topPaddingHeight))
self.view.addSubview(self.topPadding)
self.dateFormatter.dateFormat = "yyyy"
self.yearTextField = UITextField(frame: CGRect(x: viewWidth / 3, y: topPaddingHeight / 2, width: viewWidth / 3, height: yearLabelHeight))
self.yearTextField.textAlignment = .center
self.yearTextField.text = self.dateFormatter.string(from: Date())
self.yearTextField.delegate = self
self.view.addSubview(self.yearTextField)
/* Change Initial Colors */
initializeAppearance()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
/* Textfield Delegate Functions */
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField != self.yearTextField {
return true
}
let newSubjectString = (textField.text! as NSString).replacingCharacters(in: range, with: string)
let newCharacters = CharacterSet(charactersIn: newSubjectString as String)
if !CharacterSet.decimalDigits.isSuperset(of: newCharacters) {
/* New characters are not numeric characters */
return false
} else if newSubjectString.count > 4 {
/* Year field too long - should only be 4 characters */
return false
} else if newSubjectString.count == self.kMaxYearCharactersCount {
/* Checks if the year is valid */
textField.text = newSubjectString
self.view.endEditing(true)
return shouldDisplayYear()
} else {
return true
}
}
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
if textField != self.yearTextField {
return true
}
let _ = shouldDisplayYear()
return true
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField != self.yearTextField {
return true
}
self.view.endEditing(true)
return true
}
////////////////////////////// PRIVATE //////////////////////////////
/* Initializes all colors and formats */
fileprivate func initializeAppearance() {
self.calendar.appearance.headerDateFormat = headerDateFormat
self.calendar.scrollDirection = scrollDirection
self.calendar.allowsMultipleSelection = allowsMultipleSelection
self.calendar.firstWeekday = firstWeekday
self.calendar.appearance.weekdayTextColor = weekdayTextColor
self.calendar.appearance.headerTitleColor = headerTitleColor
self.calendar.appearance.eventDefaultColor = eventDefaultColor
self.calendar.appearance.eventSelectionColor = eventSelectionColor
self.calendar.appearance.selectionColor = selectionColor
self.calendar.appearance.todayColor = todayColor
self.calendar.appearance.todaySelectionColor = todaySelectionColor
self.yearTextField.textColor = yearTextColor
self.calendar.appearance.titleTodayColor = titleTodayColor
self.calendar.appearance.borderRadius = borderRadius
self.calendar.backgroundColor = calendarColor
self.calendar.calendarHeaderView.backgroundColor = headerColor
self.view.backgroundColor = headerColor
}
/* Checks if yearTextField's text is a valid year
* If it is, calendar will automatically transition to that year
* If it is not, it will trigger the textField to shake and will not change the calendar's year */
fileprivate func shouldDisplayYear() -> Bool {
self.dateFormatter.dateFormat = "yyyy"
let pageYear = dateFormatter.string(from: self.calendar.currentPage)
let maxYear = dateFormatter.string(from: self.maximumDate)
let minYear = dateFormatter.string(from: self.minimumDate)
self.dateFormatter.dateFormat = "MM"
let pageMonth = dateFormatter.string(from: self.calendar.currentPage)
/* Year must be 'kMaxYearCharactersCount' characters long */
if self.yearTextField.text?.count != self.kMaxYearCharactersCount {
shakeTextFieldAnimation(textField: self.yearTextField)
self.yearTextField.text = pageYear
return false
}
self.dateFormatter.dateFormat = "yyyy-MM-dd"
let newDate: Date! = self.dateFormatter.date(from: "\(self.yearTextField.text!)-\(pageMonth)-01")
/* Check if the newDate is within maximumDate/minimumDate
* Set the page accordingly
* If the newDate is not whithing the maximumDate/minimumDate, shake the textField and return false */
if newDate <= self.maximumDate && newDate >= self.minimumDate {
self.calendar.setCurrentPage(newDate, animated: true)
} else if newDate > self.maximumDate && self.yearTextField.text == maxYear {
self.calendar.setCurrentPage(self.maximumDate, animated: true)
} else if newDate < self.minimumDate && yearTextField.text == minYear {
self.calendar.setCurrentPage(self.minimumDate, animated: true)
} else {
shakeTextFieldAnimation(textField: yearTextField)
yearTextField.text = pageYear
return false
}
return true
}
/* TextField Animation */
fileprivate func shakeTextFieldAnimation(textField: UITextField) {
let animation = CABasicAnimation(keyPath: "position")
animation.duration = 0.07
animation.repeatCount = 4
animation.autoreverses = true
animation.fromValue = NSValue(cgPoint: CGPoint(x: textField.center.x - 10, y: textField.center.y))
animation.toValue = NSValue(cgPoint: CGPoint(x: textField.center.x + 10, y: textField.center.y))
textField.layer.add(animation, forKey: "position")
}
////////////////////////////// PUBLIC //////////////////////////////
func selectDefaultDate() {
let currentDate = Date()
if currentDate > self.minimumDate && currentDate < self.maximumDate {
self.calendar.select(currentDate)
} else {
self.calendar.select(self.maximumDate)
}
}
func selectDate(date: Date) {
calendar.select(date)
}
func selectedDate() -> Date {
return self.calendar.selectedDate!
}
}
extension CalendarView: UITextFieldDelegate {
private func checkTextInputLimit(_ string: String, limit: Int, charSet: CharacterSet, allowAlpha: Bool) -> Bool {
if !allowAlpha {
if !CharacterSet.decimalDigits.isSuperset(of: charSet) {
return false
}
}
if string.count > limit {
return false
}
return true
}
}
extension CalendarView: FSCalendarDelegate {
/* FSCalendar Delegate Functions */
func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
if let _ = delegate {
delegate!.didSelect?(date: date)
}
}
func calendar(_ calendar: FSCalendar, willDisplay cell: FSCalendarCell, for date: Date, at monthPosition: FSCalendarMonthPosition) {
// This function is run multiple times when the calendar transitions (for every day of the month)
// If you display Jan. 2017, the page will also display some dates from Dec. 2016 which can cause the logic to print "2016" instead of "2017"
// depending on the last cell that this function calls
// Because of this, we have to check for a date somewhere in the middle of the month to pull the page's year
// We force the year to only change when this function hits the 20th day cell of the page to guarantee the correct year to be printed
self.dateFormatter.dateFormat = "dd"
if self.dateFormatter.string(from: date) == "20" {
self.dateFormatter.dateFormat = "yyyy"
self.yearTextField.text = dateFormatter.string(from: date)
}
}
func maximumDate(for calendar: FSCalendar) -> Date {
if let date = delegate?.maximumDate?() {
self.maximumDate = date
return date
}
return self.maximumDate
}
func minimumDate(for calendar: FSCalendar) -> Date {
if let date = delegate?.minimumDate?() {
self.minimumDate = date
return date
}
return self.minimumDate
}
}