Koyomi 1.2.7

Koyomi 1.2.7

测试经过测试
Lang语言 SwiftSwift
许可证 MIT
发布最后发布2018年2月
SPM支持 SPM

Shohei维护。



Koyomi 1.2.7

  • shoheiyokoyama

Koyomi

Koyomi 是一个用 Swift 编写的简单日历视图框架,适用于 iOS📆

内容

:octocat: 特性

Awesome

  • 简单日历视图📆
  • 易于使用😎
  • 可在任何外观属性中定制
  • 可选择的日历
  • 完整的 README
  • [x] 支持 @IBDesignable@IBInspectable
  • [x] 与 Carthage 兼容
  • [x] 支持 Swift 2.3.
  • [x] 支持 Swift 3.0

演示应用程序

打开 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)

隐藏其他月份的天数

如果想要隐藏其他月份的日子,设置isHiddenOtherMonthtrue。其他月份的日子不会显示,用户也无法选择。

    koyomi.isHiddenOtherMonth = true

获取当前月份字符串

    let currentDateString = koyomi.currentDateString()

注意

如果想要更改currentDateStringdateFormat,设置参数为格式。currentDateString(withFormat: "MM/yyyy")

currentDateString的默认dateFormatM/yyyy

日期的选择状态

您可以通过样式配置SelectionMode

SelectionMode具有嵌套枚举类型:SequenceStyleStyle

    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

突出显示特定天数

您可以在特定天改变dayColordayBackgroundColor

    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

如果您想使用KoyomiDelegate,设置calendarDelegate为目标。

    koyomi.calendarDelegate = self

声明

koyomi(_: didSelect: forItemAt)

    optional func koyomi(_ koyomi: Koyomi, didSelect date: Date, forItemAt indexPath: IndexPath) 

通知代理指定索引路径的日期已被选择。date:用户在点击单元格时选择的日期。

koyomi(_: currentDateString:)

   optional func koyomi(_ koyomi: Koyomi, currentDateString dateString: String)
    
    // if you want to change string format, use `currentDateFormat`
    koyomi.currentDateFormat = "M/yyyy"

通知代理显示的月份已更改。currentDateString:更改月份时的当前月份字符串。

koyomi(_: shouldSelectDates: to: withPeriodLength)

    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。

koyomi(_: selectionColorForItemAt: date:)

    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 使用默认颜色。

koyomi(_: fontForItemAt: date:)

    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 使用默认字体。

🔧自定义 Koyomi

自定义布局属性

    // 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 中设置 sectionSpacecellSpaceweekCellHeight

自定义文本位置

    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
    }

📝 要求

  • iOS 8.0+
  • Xcode 8.0+
  • Swift 3.0+

💻 安装

安装 Swift 2.3

请安装版本 0.1.6 或更早版本。

pod 'Koyomi', '~> 0.1.6'

使用 Koyomi 的应用程序

如果您在您的应用程序中使用 Koyomi,请打开 PR 添加到此列表!😊

贡献

查看 CONTRIBUTING 文件

☕️ 作者

shoheiyokoyama, [email protected]

🔓 许可证

Koyomi 适用于 MIT 许可证。有关更多信息,请参阅 LICENSE 文件