JXSegmentedNumberDataSource.swift 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //
  2. // JXSegmentedNumberDataSource.swift
  3. // JXSegmentedView
  4. //
  5. // Created by jiaxin on 2018/12/28.
  6. // Copyright © 2018 jiaxin. All rights reserved.
  7. //
  8. import Foundation
  9. import UIKit
  10. open class JXSegmentedNumberDataSource: JXSegmentedTitleDataSource {
  11. /// 需要和titles数组数量一致,没有数字的item填0!!!
  12. open var numbers = [Int]()
  13. /// numberLabel的宽度补偿,numberLabel真实的宽度是文字内容的宽度加上补偿的宽度
  14. open var numberWidthIncrement: CGFloat = 10
  15. /// numberLabel的背景色
  16. open var numberBackgroundColor: UIColor = .red
  17. /// numberLabel的textColor
  18. open var numberTextColor: UIColor = .white
  19. /// numberLabel的font
  20. open var numberFont: UIFont = UIFont.systemFont(ofSize: 11)
  21. /// numberLabel的默认位置是center在titleLabel的右上角,可以通过numberOffset控制X、Y轴的偏移
  22. open var numberOffset: CGPoint = CGPoint.zero
  23. /// 如果业务需要处理超过999就像是999+,就可以通过这个闭包实现。默认显示不会对number进行处理
  24. open var numberStringFormatterClosure: ((Int) -> String)?
  25. /// numberLabel的高度,默认:14
  26. open var numberHeight: CGFloat = 14
  27. deinit {
  28. numberStringFormatterClosure = nil
  29. }
  30. open override func preferredItemModelInstance() -> JXSegmentedBaseItemModel {
  31. return JXSegmentedNumberItemModel()
  32. }
  33. open override func preferredRefreshItemModel(_ itemModel: JXSegmentedBaseItemModel, at index: Int, selectedIndex: Int) {
  34. super.preferredRefreshItemModel(itemModel, at: index, selectedIndex: selectedIndex)
  35. guard let itemModel = itemModel as? JXSegmentedNumberItemModel else {
  36. return
  37. }
  38. itemModel.number = numbers[index]
  39. if numberStringFormatterClosure != nil {
  40. itemModel.numberString = numberStringFormatterClosure!(itemModel.number)
  41. }else {
  42. itemModel.numberString = "\(itemModel.number)"
  43. }
  44. itemModel.numberTextColor = numberTextColor
  45. itemModel.numberBackgroundColor = numberBackgroundColor
  46. itemModel.numberOffset = numberOffset
  47. itemModel.numberWidthIncrement = numberWidthIncrement
  48. itemModel.numberHeight = numberHeight
  49. }
  50. //MARK: - JXSegmentedViewDataSource
  51. open override func registerCellClass(in segmentedView: JXSegmentedView) {
  52. segmentedView.collectionView.register(JXSegmentedNumberCell.self, forCellWithReuseIdentifier: "cell")
  53. }
  54. open override func segmentedView(_ segmentedView: JXSegmentedView, cellForItemAt index: Int) -> JXSegmentedBaseCell {
  55. let cell = segmentedView.dequeueReusableCell(withReuseIdentifier: "cell", at: index)
  56. return cell
  57. }
  58. }