About FSCalendar

前言

FSCalendar 是一個在日曆上蠻強大的第三方套件,用 Cocoapod 就能輕鬆安裝,官方也有一些基本的教學,例如: 简书More usageDocument

不過還是有些細節的東西未被摘入,本篇單純紀錄一些自己蠻常用的功能。


calendar

語言環境

calendar.locale = Locale(identifier: "en-US")


header background

calendar.calendarHeaderView.backgroundColor = .green



顯示的日期

calendar.placeholderType = .none
calendar.placeholderType = .fillSixRows
calendar.placeholderType = .fillHeadTail




calendar.appearance

header 日期格式

calendar.appearance.headerDateFormat = "yyyy-MM"


隱藏在 header 左右兩旁的上下個月

calendar.appearance..headerMinimumDissolvedAlpha = 0.0


weekday、header 大小寫

calendar.appearance.caseOptions = [.weekdayUsesUpperCase, .weekdayUsesSingleUpperCase, .headerUsesUpperCase]


讓我花最多時間的是日期下方的 image 及點擊日期所產生的 shapeLayer。image 會因為圖片原始大小而變大,但是我找不到 contentInset 可以設定,目前採用的做法是用程式碼改變圖片大小(但是解析度會有些影響)。

func resizeImage(targetSize: CGSize) -> UIImage {
let size = self.size
let widthRatio = targetSize.width / size.width
let heightRatio = targetSize.height / size.height
var newSize: CGSize
if(widthRatio > heightRatio) {
newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
} else {
newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
}
let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)
UIGraphicsBeginImageContextWithOptions(newSize, false, UIScreen.main.scale)
self.draw(in: rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}



而 shapeLayer 其實可以在 FSCalendarCell 取得這個屬性,如下:

let cell = calendar.cell(for: Date(), at: .current)
cell.shapeLayer...



但是這樣的做法我遇到兩個狀況沒辦法控制 shapeLayer 的大小,而這兩個狀況指向同一個原因,就是利用 date 取不到對應的 cell ,目前有開這條 issue ,但是作者還沒有回應,後來就乾脆動手改原始碼,在 calendar 新增一個 isHalfShape 的屬性,然後在 shapeLayer 設定 frame 的地方作出相應的處理,如:

if([_calendar isHalfShape]){
_shapeLayer.frame = CGRectMake((self.bounds.size.width-diameter)/2 + (diameter / 2 / 2),
(titleHeight-diameter)/2 + (diameter / 2 / 2),
diameter / 2,
diameter / 2);
}else{
// Original
_shapeLayer.frame = CGRectMake((self.bounds.size.width-diameter)/2,
(titleHeight-diameter)/2,diameter, diameter);
}

詳情請點 這裡!!




LICENSE

寫到這其實讓我最不安的就是不知道 MIT LICENSE 能不能允許我發布這類的筆記文章,稍微去爬了文看到了這段「你的發行版裡包含原許可協議的聲明」,所以在此附上原作者的 LICENSE:

Copyright (c) 2013-2016 FSCalendar (https://github.com/WenchaoD/FSCalendar)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.