swiftで、fscalendarを使っていますが、任意の複数の日付の下に文字やマークを記したいのですが、どなたか教えてもらえませんか?
コード
import UIKit
import FSCalendar
import CalculateCalendarLogic
class ViewController: UIViewController,FSCalendarDelegate,FSCalendarDataSource,FSCalendarDelegateAppearance{
@IBOutlet weak var calendar: FSCalendar! override func viewDidLoad() { super.viewDidLoad() // デリゲートの設定 self.calendar.dataSource = self self.calendar.delegate = self } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } fileprivate let gregorian: Calendar = Calendar(identifier: .gregorian) fileprivate lazy var dateFormatter: DateFormatter = { let formatter = DateFormatter() formatter.dateFormat = "yyyy-MM-dd" return formatter }() // 祝日判定を行い結果を返すメソッド(True:祝日) func judgeHoliday(_ date : Date) -> Bool { //祝日判定用のカレンダークラスのインスタンス let tmpCalendar = Calendar(identifier: .gregorian) // 祝日判定を行う日にちの年、月、日を取得 let year = tmpCalendar.component(.year, from: date) let month = tmpCalendar.component(.month, from: date) let day = tmpCalendar.component(.day, from: date) // CalculateCalendarLogic():祝日判定のインスタンスの生成 let holiday = CalculateCalendarLogic() return holiday.judgeJapaneseHoliday(year: year, month: month, day: day) } // date型 -> 年月日をIntで取得 func getDay(_ date:Date) -> (Int,Int,Int){ let tmpCalendar = Calendar(identifier: .gregorian) let year = tmpCalendar.component(.year, from: date) let month = tmpCalendar.component(.month, from: date) let day = tmpCalendar.component(.day, from: date) return (year,month,day) } //曜日判定(日曜日:1 〜 土曜日:7) func getWeekIdx(_ date: Date) -> Int{ let tmpCalendar = Calendar(identifier: .gregorian) return tmpCalendar.component(.weekday, from: date) } // 特別な日 let specialDays: [String] = ["2022/01/19","2022/01/26","2022/02/02","2022/02/09"] let specialDays2: [String] = ["2022/01/02","2022/01/03","2022/01/04","2022/12/30","2022/12/31","2023/01/03","2023/01/04"] // 特別な日か判定 func judgeSpecialDay(_ date: Date) -> Bool { let dateFormatter = DateFormatter() dateFormatter.dateFormat = "y/MM/dd" let dateString = dateFormatter.string(from: date) return specialDays.contains(dateString) } func judgeSpecialDay2(_ date: Date) -> Bool { let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy/MM/dd" let dateString = dateFormatter.string(from: date) return specialDays2.contains(dateString) } // 土日や祝日の日の文字色を変える func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, titleDefaultColorFor date: Date) -> UIColor? { //祝日判定をする(祝日は赤色で表示する) if self.judgeHoliday(date){ return UIColor.red } //土日の判定を行う(土曜日は青色、日曜日は赤色で表示する) let weekday = self.getWeekIdx(date) if weekday == 1 { //日曜日 return UIColor.red } else if judgeSpecialDay2(date) { return UIColor.red } else if weekday == 7 { //土曜日 return UIColor.blue } else if judgeSpecialDay(date) { return UIColor.blue } return nil }
}
あなたの回答
tips
プレビュー