I am using FSCalendar and its displaying only default holidays like on Sunday (The weekend) and so I want to add my own holidays in it and change color.
So please anyone knows how to add custom holidays????
I think you have to maintain holidays list on code level like model class(Array) or you can use the database for storing holidays list. At the render time only adopt the FSCalendarDelegateAppearance delegate. This call a delegate function for every date.
See example i think it can help you,
var datesWithHolidays = ["2018/09/03", "2018/10/06", "2018/09/12", "2018/10/25"]
//This is the delegate method
func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, fillDefaultColorFor date: Date) -> UIColor? {
let dateString = self.dateFormatter1.string(from: date)
if self.datesWithHolidays.contains(dateString) {
return UIColor.green
}
return nil
}
Related
I am doing an app with calendar with swift. I want to costumize to look like this:
The problem is here :
func calendarView(_ view: YMCalendarView, dateRangeForEventAtIndex index: Int, date: Date) -> DateRange? {
if calendar.isDateInToday(date)
|| calendar.isDate(date, inSameDayAs: calendar.endOfMonthForDate(date))
|| calendar.isDate(date, inSameDayAs: calendar.startOfMonthForDate(date))
{
return DateRange(start: date, end: calendar.endOfDayForDate(date))
}
return nil
}
The code above is ok to color the first day of month, last, and the current day but i want to color ex: from 12.09.2018 to 15.09.2018 how to do? Help me please if you can.
How does your event model look like? I guess it has a start and an end date, right? Fetch the event object by index in dateRangeForEventAtIndex, then return DateRange(start: event.startDate, end: event.endDate).
you can use UserDefault to save that date where you want to start and then compare to your current date or etc if match then set color green else nil
I am using https://github.com/WenchaoD/FSCalendar in my project . MaximumSelectedDate is a read-only property .Then how can disable future dates ?
You should be using the delegate method to address this
func maximumDate(for calendar: FSCalendar) -> Date {
return Date()
}
A workaround could be to edit FSCalendar method file. First make a bool variable, say isAllowedToLimitFutureDates and a string variable maxValidFutureDateAsString then change line 172 of this link to:
if(!isAllowedToLimitFutureDates)
{
_maximumDate = [self.formatter dateFromString:#"2099-12-31"];
}
else
{
_maximumDate = maxValidFutureDateAsString; // say "2017-03-13"
}
So when you want to limit the dates set isAllowedToLimitFutureDates = true.
Similar approach to line 1707.
In case you cannot edit file and used PODs, then you can customize this control and override them.
Hope that helps!
#Devraj answer is correct, there are delegates for both minimum and maximum dates, all you need to do is implementing the proper one (the later one in your case) in the controller that's conforming to FSCalendarDelegate and that'll do the trick.
func maximumDateForCalendar(calendar: FSCalendar) -> NSDate {
return NSDate() // NSDate of your choosing here
}
for Swift 3
fileprivate lazy var dateFormatter2: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "dd-MM-yyyy"
return formatter }()
let today = dateFormatter2.string(from: calendar.selectedDate!)
let dateObj = dateFormatter2.date(from: today)
if dateObj! > calendar.today! {
// Your logic here
}
I used FSCalendar(https://github.com/WenchaoD/FSCalendar) in my project. If user click repeat button, the events repeats everyday. I want to show it on calendar which is in my application. How can I do it?
If you want to select date then below method will be helpful,
where calendar is outlet of FSCalendar.
calendar.select(calendar.today)
And if you want to select multiple dates then the best way would be to use event dots, and that can be possible through below delegate method of FSCalendar Data source,
func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int
{
//number of dots you want to show
}
or if you really want to show dates selected, you can use below method smartly and returning different colours for dates that you want to select,
public func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, fillDefaultColorFor date: Date) -> UIColor?
{
if your date
{
//return the colour for your dates
}
else
{
//return default colour
}
}
Hope, this answer helps you.
For Remainder app you need to do or use following thing.
1.Realm or Core Data - for Data persistency.
2.local notification - to notify user that some event is occured.
you need to use UILocalNotification that have one property fireDate property.
I don't know how you're persisting that information (the scheduled date), what i can tell you is that you can easily add a dot (or more if you'd like, just change the number) with the following delegate:
`func calendar(calendar: FSCalendar, numberOfEventsForDate date: NSDate) -> Int {
let formatter = NSDateFormatter()
formatter.dateFormat = "MM-DD"
let date1 = formatter.stringFromDate(yourDate)
let date2 = formatter.stringFromDate(date)
return date1 == date2 ? 1 : 0
}`
This method is one of FSCalendar delegates and it iterates all days in the current month (each one of them represented in date variable). In the example above i'm comparing if the any given date is equal to yourDate variable which has to be a NSDate as well by transforming both of them into Strings via NSDateFormatter in Month-Day format so the comparison is equal for all months. If true, return 1 (this indicates 1 dot, you may change it for more if you want to), otherwise return 0 dots below the specific date.
This code is Swift 2.3 compliance.
I am using FSCalendar, and I want to highlight dates day wise as am getting Wednesday from server and i want to highlight all Wednesday to be selected on FSCalendar of current month or year. Please solve this. Thank You.
FSCalendar has a delegate for cases like this, it's called titleDefaultColorForDate and you can use it this way:
func calendar(calendar: FSCalendar, appearance: FSCalendarAppearance, titleDefaultColorForDate date: NSDate) -> UIColor? {
return calendar.gregorian.component(.Weekday, fromDate: date) == 4 ? UIColor.blueColor() : UIColor.blackColor()
}
Let me elaborate: What this delegate method does is return a UIColor based on the date that's passed by reference when the view loads, in other words, every time a month loads this method is called 30+ times with every single date in it. Here i made the condition to return true if the given date was equal to 4 since that's how Wednesday is represented by this helper
calendar.gregorian.component(.Weekday, fromDate: date) // calendar is my FSCalendar outlet.
To summarize, Wednesday are blue and every other week day is black.
PS: Notice Wednesday = 4 in my case since my week begins on Sunday, otherwise you must do this calculation based on your starting day.
I implemented a UIDatePicker into my app. If I change the default date after the date picker was popping up, the date picker gives a valid date back. But if I'd like to choose the current date (I don't have to change the date picker) the date picker doesn't give a date back. Does someone knows a solution for this in Swift?
//Date Picker config.
func datePickerChanged(datePicker:UIDatePicker) {
var dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
var strDate = dateFormatter.stringFromDate(datePicker.date)
dateLabel.text = strDate
}
You have a callback when you change the date (valueChanged). So it is normal you don't get it when you just dismiss the picker without changing the date.
You have to know the date before you present your date picker, in case you don't set it, or if you want to cancel your selection if you handle it.
Create a var (I generally call it now ) where you store the currentDate (NSDate()) and set it to the date property of your UIDatePicker, so you know the date when the picker was presented since the default datePicker's date displayed is now.
If you set UIDatePicker's date in code, it doesn't run valueChanged events. You have to extract the date string update into a separate method and update it directly.
Also, don't treat the UIDatePicker as the sole date value source. Keep the model separate.
i.e.
var date = NSDate()
func datePickerChanged(datePicker:UIDatePicker) {
self.date = datePicker.date
self.updateDateStringWithDate(self.date)
}
func resetDate() {
self.date = NSDate()
self.datePicker.date = self.date
self.updateDateStringWithDate(self.date)
}
func updateDateStringWithDate(date: NSDate) {
var dateFormatter = UIDateFormatter()
dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
var strDate = dateFormatter.stringFromDate(date)
dateLabel.text = strDate
}
Also as a side note, don't create a new UIDateFormatter every time it's needed, because its initialisation is relatively expensive.