373 lines
7.7 KiB
Markdown
373 lines
7.7 KiB
Markdown
# More usage
|
|
|
|
### If you want `FSCalendar` to scroll vertically
|
|
|
|
* Objective - c
|
|
|
|
```objective-c
|
|
_calendar.scrollDirection = FSCalendarScrollDirectionVertical;
|
|
```
|
|
|
|
* Swift
|
|
|
|
```swift
|
|
calendar.scrollDirection = .vertical
|
|
```
|
|
|
|

|
|
|
|
### If you want `FSCalendar` to scroll horizontally (Default)
|
|
|
|
* Objective - c
|
|
|
|
```objective-c
|
|
_calendar.scrollDirection = FSCalendarScrollDirectionHorizontal; // By default
|
|
```
|
|
|
|
* Swift
|
|
|
|
```swift
|
|
calendar.scrollDirection = .Horizontal
|
|
```
|
|
|
|

|
|
|
|
## Focus on selected date on ***week mode***
|
|

|
|
|
|
* There are cases such as `Changing Scope`、`Changing month while showsPlaceholders is false` will trigger a bounds changing of FSCalendar, make sure the frame is adjusted in `-calendar:boundingRectWillChange:animated:`
|
|
|
|
```objc
|
|
// For autoLayout
|
|
- (void)calendar:(FSCalendar *)calendar boundingRectWillChange:(CGRect)bounds animated:(BOOL)animated
|
|
{
|
|
_calendarHeightConstraint.constant = CGRectGetHeight(bounds);
|
|
[self.view layoutIfNeeded];
|
|
}
|
|
```
|
|
|
|
```objc
|
|
// For manual layout
|
|
- (void)calendar:(FSCalendar *)calendar boundingRectWillChange:(CGRect)bounds animated:(BOOL)animated
|
|
{
|
|
calendar.frame = (CGRect){calendar.frame.origin,bounds.size};
|
|
}
|
|
```
|
|
|
|
### For week mode
|
|
|
|
* Objective-C
|
|
|
|
```objective-c
|
|
_calendar.scope = FSCalendarScopeWeek;
|
|
```
|
|
|
|
* Swift
|
|
|
|
```swift
|
|
calendar.scope = .week
|
|
```
|
|
|
|
### For month mode
|
|
|
|
* Objective - c
|
|
|
|
```objective-c
|
|
_calendar.scope = FSCalendarScopeMonth; // By default
|
|
```
|
|
|
|
* Swift
|
|
|
|
```swift
|
|
calendar.scope = .month
|
|
```
|
|
|
|

|
|
|
|
### To select more than one date
|
|
|
|
* Objective-C
|
|
|
|
```objective-c
|
|
calendar.allowsMultipleSelection = YES;
|
|
```
|
|
|
|
* Swift
|
|
|
|
```swift
|
|
calendar.allowsMultipleSelection = true;
|
|
```
|
|
|
|
|
|

|
|
|
|
### If you want `FSCalendar` to use `Monday` as the first column (or any other weekday)
|
|
|
|
* Objective-C
|
|
|
|
```objective-c
|
|
_calendar.firstWeekday = 2;
|
|
```
|
|
|
|
* Swift
|
|
|
|
```swift
|
|
calendar.firstWeekday = 2
|
|
```
|
|
|
|

|
|
|
|
|
|
### The date format of header can be customized
|
|
|
|
* Objective-C
|
|
|
|
```objective-c
|
|
calendar.appearance.headerDateFormat = @"MMM yy";
|
|
```
|
|
|
|
* Swift
|
|
|
|
```swift
|
|
calendar.appearance.headerDateFormat = "MMM yy"
|
|
```
|
|
|
|

|
|
|
|
### You can define the appearance
|
|
|
|
* Objective-c
|
|
|
|
```objective-c
|
|
_calendar.appearance.weekdayTextColor = [UIColor redColor];
|
|
_calendar.appearance.headerTitleColor = [UIColor redColor];
|
|
_calendar.appearance.eventColor = [UIColor greenColor];
|
|
_calendar.appearance.selectionColor = [UIColor blueColor];
|
|
_calendar.appearance.todayColor = [UIColor orangeColor];
|
|
_calendar.appearance.todaySelectionColor = [UIColor blackColor];
|
|
```
|
|
|
|
* Swift
|
|
|
|
```objective-c
|
|
calendar.appearance.weekdayTextColor = UIColor.redColor
|
|
calendar.appearance.headerTitleColor = UIColor.redColor
|
|
calendar.appearance.eventColor = UIColor.greenColor
|
|
calendar.appearance.selectionColor = UIColor.blueColor
|
|
calendar.appearance.todayColor = UIColor.orangeColor
|
|
calendar.appearance.todaySelectionColor = UIColor.blackColor
|
|
```
|
|
|
|
|
|

|
|
|
|
### The day shape doesn't have to be a circle
|
|
|
|
* Objective-C
|
|
|
|
```objective-c
|
|
calendar.appearance.borderRadius = 0
|
|
```
|
|
|
|
* Swift
|
|
|
|
```swift
|
|
calendar.appearance.borderRadius = 0
|
|
```
|
|
|
|

|
|
|
|
### `FSCalendar` can show subtitle for each day
|
|
|
|
* Objective-C
|
|
|
|
```objective-c
|
|
// FSCalendarDataSource
|
|
- (NSString *)calendar:(FSCalendar *)calendar subtitleForDate:(NSDate *)date
|
|
{
|
|
return yourSubtitle;
|
|
}
|
|
```
|
|
|
|
* Swift
|
|
|
|
```swift
|
|
// FSCalendarDataSource
|
|
func calendar(_ calendar: FSCalendar!, subtitleFor date: NSDate!) -> String! {
|
|
return yourSubtitle
|
|
}
|
|
```
|
|
|
|

|
|
<br/>
|
|

|
|
|
|
### And event dot for some days
|
|
|
|
* Objective-C
|
|
|
|
```objective-c
|
|
// FSCalendarDataSource
|
|
- (BOOL)calendar:(FSCalendar *)calendar hasEventForDate:(NSDate *)date
|
|
{
|
|
return shouldShowEventDot;
|
|
}
|
|
```
|
|
|
|
* Swift
|
|
|
|
```swift
|
|
// FSCalendarDataSource
|
|
func calendar(calendar: FSCalendar!, hasEventForDate date: NSDate!) -> Bool {
|
|
return shouldShowEventDot
|
|
}
|
|
```
|
|
|
|
### Or image for some days
|
|
|
|
* Objective - c
|
|
|
|
```objective-c
|
|
// FSCalendarDataSource
|
|
- (UIImage *)calendar:(FSCalendar *)calendar imageForDate:(NSDate *)date
|
|
{
|
|
return anyImage;
|
|
}
|
|
```
|
|
|
|
* Swift
|
|
|
|
```swift
|
|
// FSCalendarDataSource
|
|
func calendar(_ calendar: FSCalendar!, imageFor date: NSDate!) -> UIImage! {
|
|
return anyImage
|
|
}
|
|
```
|
|
|
|

|
|
|
|
#### You can hide top and bottom borders
|
|
|
|
* Objective-C
|
|
|
|
```objective-c
|
|
calendar.clipsToBounds = YES
|
|
```
|
|
|
|
* Swift
|
|
|
|
```swift
|
|
calendar.clipsToBounds = true
|
|
```
|
|
|
|

|
|
|
|
#### There are left and right boundaries
|
|
|
|
```objective-c
|
|
// FSCalendarDataSource
|
|
- (NSDate *)minimumDateForCalendar:(FSCalendar *)calendar
|
|
{
|
|
return yourMinimumDate;
|
|
}
|
|
|
|
- (NSDate *)maximumDateForCalendar:(FSCalendar *)calendar
|
|
{
|
|
return yourMaximumDate;
|
|
}
|
|
```
|
|
|
|
### You can do something when a date is selected
|
|
|
|
* Objective - c
|
|
|
|
```objective-c
|
|
// FSCalendarDelegate
|
|
- (void)calendar:(FSCalendar *)calendar didSelectDate:(NSDate *)date
|
|
{
|
|
// Do something
|
|
}
|
|
```
|
|
|
|
* Swift
|
|
|
|
```swift
|
|
// FSCalendarDelegate
|
|
func calendar(calendar: FSCalendar!, didSelectDate date: NSDate!) {
|
|
|
|
}
|
|
|
|
```
|
|
|
|
### You can prevent it from being selected
|
|
|
|
* Objective - c
|
|
|
|
```objective-c
|
|
// FSCalendarDelegate
|
|
- (BOOL)calendar:(FSCalendar *)calendar shouldSelectDate:(NSDate *)date
|
|
{
|
|
if ([dateShouldNotBeSelected]) {
|
|
return NO;
|
|
}
|
|
return YES;
|
|
}
|
|
```
|
|
|
|
* Swift
|
|
|
|
```swift
|
|
func calendar(calendar: FSCalendar!, shouldSelectDate date: NSDate!) -> Bool {
|
|
if dateShouldNotBeSelected {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
```
|
|
|
|
|
|
### You will get notified when `FSCalendar` changes the month or week
|
|
|
|
* Objective-C
|
|
|
|
```objective-c
|
|
- (void)calendarCurrentMonthDidChange:(FSCalendar *)calendar
|
|
{
|
|
// Do something
|
|
}
|
|
```
|
|
|
|
* Swift
|
|
|
|
```swift
|
|
func calendarCurrentMonthDidChange(_ calendar: FSCalendar!) {
|
|
// Do something
|
|
}
|
|
```
|
|
|
|
* `fakeSubtitles` and `fakedSelectedDay` is only used for preview in Interface Builder
|
|
|
|
|
|
## Q & A
|
|
|
|
### Q: What if I don't need the `today` circle?
|
|
Just nill-out the `today` property like:
|
|
|
|
```objc
|
|
self.calendar.today = nil;
|
|
```
|
|
|
|
### Q: Can we hide this?
|
|

|
|
|
|
```objc
|
|
self.calendar.appearance.headerMinimumDissolvedAlpha = 0.0;
|
|
```
|
|
|
|
### Q: Can we refresh the calendar after a network request?
|
|
Yes. Like UITableView or UICollectionView, there is also `reloadData` in `FSCalendar`;
|
|
|
|
```objc
|
|
[self.calendar reloadData];
|
|
```
|