AKExcelDateManager.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //
  2. // AKExcelDateManager.swift
  3. // YunYingSwift
  4. //
  5. // Created by AlasKu on 17/2/10.
  6. // Copyright © 2017年 innostic. All rights reserved.
  7. //
  8. import UIKit
  9. class AKExcelDataManager: NSObject {
  10. //MARK: - Properties
  11. /// excelView
  12. var excelView : AKExcelView?
  13. /// AllExcel Data
  14. var dataArray : [[String]]?
  15. /// freezeCollection Width
  16. var freezeWidth : CGFloat = 0
  17. /// freezeColectionView cells Size
  18. var freezeItemSize = [String]()
  19. /// slideCollectionView Cells Size
  20. var slideItemSize = [String]()
  21. /// slideCollectionView Cells Size
  22. var slideWidth : CGFloat = 0
  23. /// headFreezeCollectionView Data
  24. var headFreezeData = [String]()
  25. /// headSlideCollectionView Data
  26. var headSlideData = [String]()
  27. /// contentFreezeCollectionView Data
  28. var contentFreezeData = [[String]]()
  29. /// contentSlideCollectionView Data
  30. var contentSlideData = [[String]]()
  31. /// 支持颜色
  32. var contentSlideDataTextColor = [[UIColor]]()
  33. /// slideItemOffSetX
  34. var slideItemOffSetX = [CGFloat]()
  35. private func resetData() {
  36. dataArray?.removeAll()
  37. freezeWidth = 0
  38. freezeItemSize.removeAll()
  39. slideItemSize.removeAll()
  40. slideWidth = 0
  41. headFreezeData.removeAll()
  42. headSlideData.removeAll()
  43. contentFreezeData.removeAll()
  44. contentSlideData.removeAll()
  45. contentSlideDataTextColor.removeAll()
  46. slideItemOffSetX.removeAll()
  47. }
  48. //MARK: - Private Method
  49. private func loadData() {
  50. var arrM = [[String]]()
  51. if let headerTitles = excelView?.headerTitles {
  52. arrM.append(headerTitles)
  53. }
  54. if let contentData = excelView?.contentData {
  55. for model in contentData {
  56. arrM.append(model.valuesFor(excelView?.properties))
  57. }
  58. }
  59. dataArray = arrM
  60. }
  61. private func configData() {
  62. guard let excelView = excelView,
  63. let dataArray = dataArray,
  64. dataArray.count > (excelView.headerTitles == nil ? 0 : 1) else { return }
  65. if let _ = excelView.headerTitles {
  66. headFreezeData = dataArray[0].prefix(upTo: excelView.leftFreezeColumn).compactMap({ $0 })
  67. headSlideData = dataArray[0].suffix(from: excelView.leftFreezeColumn).compactMap({ $0 })
  68. contentFreezeData = dataArray[1..<dataArray.count].compactMap({ $0.prefix(upTo: excelView.leftFreezeColumn).compactMap({ $0 }) })
  69. contentSlideData = dataArray[1..<dataArray.count].compactMap({ $0.suffix(from: excelView.leftFreezeColumn).compactMap({ $0 }) })
  70. } else {
  71. contentFreezeData = dataArray.prefix(upTo: excelView.leftFreezeColumn).compactMap({ $0 })
  72. contentSlideData = dataArray.suffix(from: excelView.leftFreezeColumn).compactMap({ $0 })
  73. }
  74. }
  75. private func caculateWidths() {
  76. guard let excelView = excelView,
  77. let dataArray = dataArray,
  78. dataArray.count > (excelView.headerTitles == nil ? 0 : 1) else { return }
  79. var fItemSize = [String]()
  80. var sItemSize = [String]()
  81. let col = dataArray.first!.count // 标题那一行或者数据第一行
  82. for i in 0 ..< col {
  83. var colW = CGFloat()
  84. for j in 0 ..< dataArray.count {
  85. let value = dataArray[j][i]
  86. var size = value.getTextSize(font: excelView.contentTextFontSize, size: CGSize(width: excelView.itemMaxWidth, height: excelView.itemHeight))
  87. if j == 0 && excelView.headerTitles != nil {
  88. size = value.getTextSize(font: excelView.headerTextFontSize, size: CGSize(width: excelView.itemMaxWidth, height: excelView.itemHeight))
  89. }
  90. if let columnWidthSetting = excelView.columnWidthSetting,
  91. let setWidth = columnWidthSetting[i] {
  92. size = CGSize(width: setWidth, height: excelView.itemHeight)
  93. }
  94. let targetWidth = size.width + 2 * excelView.textMargin
  95. // 若这个字符串宽度大于上一个单元格的宽度,目的是为了统一每一列的宽度
  96. if targetWidth >= colW {
  97. colW = targetWidth
  98. }
  99. colW = max(excelView.itemMinWidth, min(excelView.itemMaxWidth, colW))
  100. }
  101. // 滑动scroll节点
  102. slideItemOffSetX.append(slideWidth)
  103. if i < excelView.leftFreezeColumn {
  104. fItemSize.append(NSCoder.string(for: CGSize(width: colW, height: excelView.itemHeight - 1)))
  105. freezeWidth += colW + 1 // 这里加1的原因是为了解决浮点数计算误差
  106. dPrint("fItemSize is \(NSCoder.string(for: CGSize(width: colW, height: excelView.itemHeight - 1)))")
  107. dPrint("fItemSize is \(freezeWidth)")
  108. } else {
  109. sItemSize.append(NSCoder.string(for: CGSize(width: colW, height: excelView.itemHeight - 1)))
  110. slideWidth += colW + 1 // 这里加1的原因是为了解决浮点数计算误差
  111. dPrint("sItemSize is \(NSCoder.string(for: CGSize(width: colW, height: excelView.itemHeight - 1)))")
  112. dPrint("slideWidth is \(slideWidth)")
  113. }
  114. }
  115. freezeItemSize = fItemSize
  116. slideItemSize = sItemSize
  117. }
  118. //MARK: - Public Method
  119. func caculateData() {
  120. resetData()
  121. loadData()
  122. configData()
  123. caculateWidths()
  124. }
  125. }
  126. //MARK: - String Extension
  127. extension String {
  128. func getTextSize(font:UIFont,size:CGSize) -> CGSize {
  129. let dic = NSDictionary(object: font, forKey: NSAttributedString.Key.font as NSCopying)
  130. let stringSize = self.boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: dic as? [NSAttributedString.Key : Any] , context:nil).size
  131. return stringSize
  132. }
  133. }
  134. extension NSObject
  135. {
  136. func propertyNames() -> [String] {
  137. return Mirror(reflecting: self).children.compactMap { $0.label }
  138. }
  139. func valueFor(_ property:String) -> String? {
  140. var value : String?
  141. for case let (label?, anyValue) in Mirror(reflecting:self).children {
  142. if label.isEqual(property) {
  143. value = anyValue as? String
  144. }
  145. }
  146. return value
  147. }
  148. func valuesFor(_ properties:[String]?) -> [String] {
  149. if let pros = properties {
  150. var arrM = [String]()
  151. for pro in pros {
  152. arrM.append(valueFor(pro)!)
  153. }
  154. return arrM
  155. }
  156. var values = [String]()
  157. for case let (_?, anyValue) in Mirror(reflecting:self).children {
  158. values.append(anyValue as? String ?? "")
  159. }
  160. return values
  161. }
  162. }