KMBaseTextFieldModel.swift 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // KMBaseTextFieldModel.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2022/12/23.
  6. //
  7. import Cocoa
  8. enum KMBaseTextFieldUnit {
  9. case mm // 毫米
  10. case cm // 厘米
  11. case inch // 英寸
  12. }
  13. enum KMBaseTextFieldInputType {
  14. case none
  15. case int //整形
  16. case float //带一位小数点
  17. case float_2 //带二位小数点
  18. case percent //带百分号
  19. }
  20. class KMBaseTextFieldModel: NSObject {
  21. var value: String = ""
  22. var stringValue: String {
  23. return self.valueConversion(value: self.value, type: self.inputType)
  24. }
  25. var placeholderString: String = ""
  26. var isEnabled: Bool?
  27. var minLen: Int? = 0
  28. var maxLen: Int = Int.max
  29. var minValue: Int = Int.min
  30. var maxValue: Int = Int.max
  31. var onlyNumber: Bool = true
  32. var inputType: KMBaseTextFieldInputType = .none
  33. var specialChart: String = ""
  34. var isCanNull: Bool = false //问题可以为 nil 字符串
  35. }
  36. extension KMBaseTextFieldModel {
  37. /**
  38. @abstract 单位转换
  39. @value 转换值float (方便转换) 像素
  40. @unit 转换类型
  41. */
  42. func unitConversion(value: Float, unit: KMBaseTextFieldUnit) -> Float {
  43. var scale: Float = 1.0
  44. var result = value
  45. switch unit {
  46. case .cm:
  47. scale = 595.0 / 21.0
  48. case .mm:
  49. scale = 595.0 / 210.0
  50. case .inch:
  51. scale = (595.0 / 21.0) * 2.54
  52. }
  53. result = result * scale
  54. return result
  55. }
  56. /**
  57. @abstract 数值转换
  58. @value 转换值float (方便转换) 参数
  59. @type 转换类型
  60. */
  61. func valueConversion(value: String, type: KMBaseTextFieldInputType) -> String {
  62. var string: NSString
  63. switch type {
  64. case .int:
  65. string = NSString(format: "%.0f", value)
  66. case .float:
  67. string = NSString(format: "%.1f", value)
  68. case .float_2:
  69. string = NSString(format: "%.2f", value)
  70. case .percent:
  71. string = NSString(format: "%.0f%", value)
  72. case .none:
  73. string = NSString(format: "%@", value)
  74. }
  75. return string as String
  76. }
  77. }