@@ -14,6 +14,21 @@ class NewCalendarViewController: UIViewController {
14
14
// 日本の祝祭日判定用のインスタンス
15
15
private let holidayObject : CalculateCalendarLogic = CalculateCalendarLogic ( )
16
16
17
+ private var stringDateFormatter : DateFormatter = {
18
+ let dateFormatter = DateFormatter ( )
19
+ dateFormatter. locale = Locale ( identifier: " ja_JP " )
20
+ dateFormatter. timeZone = TimeZone . current
21
+ dateFormatter. dateFormat = " yyyy/MM/dd "
22
+ return dateFormatter
23
+ } ( )
24
+
25
+ private var currentCalendar : Calendar = {
26
+ var calendar = Calendar . current
27
+ calendar. locale = Locale ( identifier: " ja_JP " )
28
+ calendar. timeZone = TimeZone . current
29
+ return calendar
30
+ } ( )
31
+
17
32
override func viewDidLoad( ) {
18
33
super. viewDidLoad ( )
19
34
setupCalendarView ( )
@@ -25,9 +40,13 @@ class NewCalendarViewController: UIViewController {
25
40
if #available( iOS 16 . 0 , * ) {
26
41
let calendarView = UICalendarView ( )
27
42
calendarView. calendar = Calendar ( identifier: . gregorian)
28
- calendarView. locale = Locale ( identifier: " ja " )
43
+ calendarView. locale = Locale ( identifier: " ja_JP " )
29
44
calendarView. delegate = self
30
-
45
+ // 例. 任意の日付を1つ選択可能にする
46
+ // 補足: 複数日付を選択する場合はこちらをする
47
+ // https://developer.apple.com/documentation/uikit/uicalendarselectionmultidate
48
+ let dateSelection = UICalendarSelectionSingleDate ( delegate: self )
49
+ calendarView. selectionBehavior = dateSelection
31
50
view. addSubview ( calendarView)
32
51
calendarView. translatesAutoresizingMaskIntoConstraints = false
33
52
calendarView. topAnchor. constraint ( equalTo: self . view. safeAreaLayoutGuide. topAnchor) . isActive = true
@@ -64,14 +83,59 @@ extension NewCalendarViewController: UICalendarViewDelegate {
64
83
@available ( iOS 16 . 0 , * )
65
84
func calendarView( _ calendarView: UICalendarView , decorationFor dateComponents: DateComponents ) -> UICalendarView . Decoration ? {
66
85
guard let targetYear = dateComponents. year,
67
- let targetMonth = dateComponents. month,
68
- let targetDay = dateComponents. day else {
86
+ let targetMonth = dateComponents. month,
87
+ let targetDay = dateComponents. day else {
69
88
return nil
70
89
}
71
- if holidayObject. judgeJapaneseHoliday ( year: targetYear, month: targetMonth, day: targetDay) {
90
+
91
+ // 注意: weekdayプロパティはUICalendarViewDelegateまで常にnilになる...
92
+ // print("dateComponents.weekday", dateComponents.weekday)
93
+
94
+ let weekday = getWeekDay ( year: targetYear, month: targetMonth, day: targetDay)
95
+ let isHoliday = holidayObject. judgeJapaneseHoliday ( year: targetYear, month: targetMonth, day: targetDay)
96
+
97
+ if isHoliday {
72
98
return . image( UIImage ( systemName: " flag.2.crossed.fill " ) , color: . orange, size: . medium)
99
+ } else if weekday == 1 {
100
+ return . default( color: . red, size: . small)
101
+ } else if weekday == 7 {
102
+ return . default( color: . blue, size: . small)
73
103
} else {
74
104
return nil
75
105
}
76
106
}
107
+
108
+ // 年・月・日から曜日(日曜日:1 ~ 土曜日:7)を取得する
109
+ private func getWeekDay( year: Int , month: Int , day: Int ) -> Int {
110
+ let formatString = " %04d/%02d/%02d "
111
+ guard let date: Date = stringDateFormatter. date ( from: String ( format: formatString, year, month, day) ) else {
112
+ return 0
113
+ }
114
+ return currentCalendar. component ( . weekday, from: date)
115
+ }
116
+ }
117
+
118
+ // MARK: - UICalendarSelectionSingleDateDelegate
119
+
120
+ extension NewCalendarViewController : UICalendarSelectionSingleDateDelegate {
121
+
122
+ // 日付を選択したタイミングで選択した日付情報を返す
123
+ @available ( iOS 16 . 0 , * )
124
+ func dateSelection( _ selection: UICalendarSelectionSingleDate , didSelectDate dateComponents: DateComponents ? ) {
125
+ guard let targetDateComponents = dateComponents else {
126
+ return
127
+ }
128
+ guard let targetYear = targetDateComponents. year,
129
+ let targetMonth = targetDateComponents. month,
130
+ let targetDay = targetDateComponents. day else {
131
+ return
132
+ }
133
+ print ( " あなたが選択した日付: " , " \( targetYear) 年 \( targetMonth) 月 \( targetDay) 日 " )
134
+ }
135
+
136
+ // 日付を選択可能な状態とするかを設定する
137
+ @available ( iOS 16 . 0 , * )
138
+ func dateSelection( _ selection: UICalendarSelectionSingleDate , canSelectDate dateComponents: DateComponents ? ) -> Bool {
139
+ return true
140
+ }
77
141
}
0 commit comments