Koyomi 是一个用 Swift 编写的简单日历视图框架,适用于 iOS
README
@IBDesignable
和 @IBInspectable
打开 Example/Koyomi.xcworkspace
并运行 Koyomi-Example
以查看简单的演示。
Koyomi 是为了易于使用而设计的
let frame = CGRect(x: 10, y : 20, width: 250, height: 300)
let koyomi = Koyomi(frame: frame, sectionSpace: 1.5, cellSpace: 0.5, inset: .zero, weekCellHeight: 25)
view.addSubview(koyomi)
Koyomi
可在 Interface Builder 中使用。将 Koyomi
@IBOutlet weak var koyomi: Koyomi!
如果您想更改显示的月份,调用 display(in: MonthType)
。其中 MonthType
由三种类型定义。
public enum MonthType { case previous, current, next }
// change month
koyomi.display(in: .next)
如果想要隐藏其他月份的日子,设置isHiddenOtherMonth
为true
。其他月份的日子不会显示,用户也无法选择。
koyomi.isHiddenOtherMonth = true
let currentDateString = koyomi.currentDateString()
注意
如果想要更改
currentDateString
的dateFormat
,设置参数为格式。currentDateString(withFormat: "MM/yyyy")
currentDateString
的默认dateFormat
为M/yyyy
您可以通过样式配置SelectionMode。
SelectionMode具有嵌套枚举类型:SequenceStyle
,Style
。
public enum SelectionMode {
case single(style: Style), multiple(style: Style), sequence(style: SequenceStyle), none
public enum SequenceStyle { case background, circle, semicircleEdge, line }
public enum Style { case background, circle, line }
}
// default selectionMode is single, circle style
public var selectionMode: SelectionMode = .single(style: .circle)
// call selectionStyle
koyomi.selectionMode = .single(style: .circle)
单选 | ![]() | ![]() | ![]() |
---|---|---|---|
SelectionMode | .single(style: .background) | .single(style: .circle) | .single(style: .line) |
多选 | ![]() | ![]() | ![]() |
---|---|---|---|
SelectionMode | .multiple(style: .background) | .multiple(style: .circle) | .multiple(style: .line) |
连续选择 | ![]() | ![]() | ![]() | ![]() |
---|---|---|---|---|
SelectionMode | .sequence(style: .background) | .sequence(style: .circle) | .sequence(style: .semicircleEdge) | .sequence(style: .line) |
在line
样式的情况下,您可以配置lineView。
public struct LineView {
public enum Position { case top, center, bottom }
public var height: CGFloat = 1
public var widthRate: CGFloat = 1 // default is 1.0 (0.0 ~ 1.0)
public var position: Position = .center
}
koyomi.selectionMode = .single(style: .line)
koyomi.lineView.height = 3
koyomi.lineView.position = .bottom
koyomi.lineView.widthRate = 0.7
注意
如果您不希望用户通过交互选择日期,将
selectionMode
设置为.none
。
您可以选择特定的日期。
let today = Date()
var components = DateComponents()
components.day = 7
let weekLaterDay = Calendar.current.date(byAdding: components, toDate: today)
koyomi.select(date: today, to: weekLaterDay)
// If want to select only one day.
koyomi.select(date: today)
// If want to select multiple day.
let dates: [Date] = [date1, date2, date3]
koyomi.select(dates: dates)
您也可以取消选择可用项。
koyomi.unselect(today, to: weekLaterDay)
// If want to unselect only one day.
koyomi.unselect(today)
// If want to unselect multiple day.
let dates: [Date] = [date1, date2, date3]
koyomi.unselect(dates: dates)
// unselect all date
koyomi.unselectAll()
您可以在选择状态下配置样式颜色和文字状态。
@IBInspectable public var selectedStyleColor: UIColor
public enum SelectedTextState { case change(UIColor), keeping }
public var selectedDayTextState: SelectedTextState
selectedDayTextState
如果您想在用户在Koyomi
中选择天时更改天的文字颜色,请将selectedDayTextState
设置为SelectedTextState.change(UIColor)
。
此外,如果您不希望在用户选择天时更改天的文字颜色,请将selectedDayTextState
设置为SelectedTextState.keeping
。
// day text color change white when selected.
koyomi.selectedDayTextState = .change(.white)
// day text color doesn't change when selected.
koyomi.selectedDayTextState = .keeping
您可以在特定天改变dayColor
和dayBackgroundColor
。
koyomi
.setDayColor(.white, of: today, to: weekLaterDay)
.setDayBackgrondColor(.black, of: today, to: weekLaterDay)
// set day color only one day.
// .setDayColor(.white, of: today)
// .setDayBackgrondColor(.black, of: today)
如果您想使用KoyomiDelegate
,设置calendarDelegate
为目标。
koyomi.calendarDelegate = self
optional func koyomi(_ koyomi: Koyomi, didSelect date: Date, forItemAt indexPath: IndexPath)
通知代理指定索引路径的日期已被选择。date
:用户在点击单元格时选择的日期。
optional func koyomi(_ koyomi: Koyomi, currentDateString dateString: String)
// if you want to change string format, use `currentDateFormat`
koyomi.currentDateFormat = "M/yyyy"
通知代理显示的月份已更改。currentDateString
:更改月份时的当前月份字符串。
optional func koyomi(_ koyomi: Koyomi, shouldSelectDates date: Date?, to toDate: Date?, withPeriodLength length: Int) -> Bool
// control date user selected.
func koyomi(_ koyomi: Koyomi, shouldSelectDates date: Date?, to toDate: Date?, withPeriodLength length: Int) -> Bool {
if invalidStartDate <= date && invalidEndDate >= toDate {
print("Your select day is invalid.")
return false
}
if length > 90 {
print("More than 90 days are invalid period.")
return false
}
return true
}
koyomi
在选择日期之前调用此方法。返回值:如果应该选择该元素,则返回true,否则返回false。如果selectionMode
不是sequence
,则始终将to
设为nil。
optional func koyomi(_ koyomi: Koyomi, selectionColorForItemAt indexPath: IndexPath, date: Date) -> UIColor?
func koyomi(_ koyomi: Koyomi, selectionColorForItemAt indexPath: IndexPath, date: Date) -> UIColor? {
return today == date ? UIColor.black : nil
}
koyomi
在设置特定日期的 selectionColor 之前会调用此方法。 返回值:返回不同于默认颜色的 UIColor
实例,或返回 nil
使用默认颜色。
func koyomi(_ koyomi: Koyomi, fontForItemAt indexPath: IndexPath, date: Date) -> UIFont?
func koyomi(_ koyomi: Koyomi, fontForItemAt indexPath: IndexPath, date: Date) -> UIFont? {
return today == date ? UIFont(name:"FuturaStd-Bold", size:16) : nil
}
koyomi
在设置特定日期的字体之前会调用此方法。 返回值:返回不同于默认字体的 UIFont
实例,或返回 nil
使用默认字体。
// Support @IBInspectable properties
@IBInspectable var sectionSpace: CGFloa
@IBInspectable var cellSpace: CGFloat
@IBInspectable var weekCellHeight: CGFloat
// Public property
public var inset: UIEdgeInsets
koyomi.inset = UIEdgeInsets(top: 0.5, left: 0.5, bottom: 0.5, right: 0.5)
在初始化或 Interface Builder 中设置 sectionSpace
、cellSpace
、weekCellHeight
。
public enum ContentPosition {
case topLeft, topCenter, topRight
case left, center, right
case bottomLeft, bottomCenter, bottomRight
case custom(x: CGFloat, y: CGFloat)
}
您可以配置文本位置。
// default is .center
koyomi.dayPosition = .topRight
koyomi.weekPosition = .center
// custom case
koyomi.dayPosition = .custom(x: 1.2, y: 2.3)
// set Day and Week Label Font
koyomi
.setDayFont(size: 12)
.setWeekFont(size: 8)
// if want to change font name,
setDayFont(fontName: ".SFUIText-Medium", size: 12)
koyomi.weeks = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")
// configure with index
koyomi.weeks.0 = "Sun"
koyomi.weeks.1 = "Mon"
koyomi.weeks.2 = "Tue"
...
// Support @IBInspectable properties
@IBInspectable public var sectionSeparatorColor: UIColor
@IBInspectable public var separatorColor: UIColor
@IBInspectable public var weekColor: UIColor
@IBInspectable public var weekdayColor: UIColor
@IBInspectable public var holidayColor: UIColor
@IBInspectable public var otherMonthColor: UIColor
@IBInspectable public var dayBackgrondColor: UIColor
@IBInspectable public var weekBackgrondColor: UIColor
@IBInspectable public var selectedStyleColor: UIColor
您可以配置许多颜色属性以用于外观。
别担心KoyomiStyle
容易地配置外观。
koyomi.style = .tealBlue
KoyomiStyle
定义了 19 种类型,其参考了 iOS Human Interface Guidelines。
enum KoyomiStyle {
// basic color style
case monotone, standard, red, orange, yellow, tealBlue, blue, purple, green, pink
// deep color style
case deepBlack, deepRed, deepOrange, deepYellow, deepTealBlue, deepBlue, deepPurple, deepGreen, deepPink
}
请安装版本 0.1.6
或更早版本。
pod 'Koyomi', '~> 0.1.6'
如果您在您的应用程序中使用 Koyomi,请打开 PR 添加到此列表!
shoheiyokoyama, [email protected]
Koyomi 适用于 MIT 许可证。有关更多信息,请参阅 LICENSE 文件。