123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- //
- // KMBaseTextFieldModel.swift
- // PDF Master
- //
- // Created by lizhe on 2022/12/23.
- //
- import Cocoa
- enum KMBaseTextFieldUnit {
- case mm // 毫米
- case cm // 厘米
- case inch // 英寸
- }
- enum KMBaseTextFieldInputType {
- case none
- case int //整形
- case float //带一位小数点
- case float_2 //带二位小数点
- case percent //带百分号
- }
- class KMBaseTextFieldModel: NSObject {
-
- var value: String = ""
- var stringValue: String {
- return self.valueConversion(value: self.value, type: self.inputType)
- }
-
- var placeholderString: String = ""
- var isEnabled: Bool?
- var minLen: Int? = 0
- var maxLen: Int = Int.max
- var minValue: Int = Int.min
- var maxValue: Int = Int.max
- var onlyNumber: Bool = true
- var inputType: KMBaseTextFieldInputType = .none
- var specialChart: String = ""
- var isCanNull: Bool = false //问题可以为 nil 字符串
- }
- extension KMBaseTextFieldModel {
- /**
- @abstract 单位转换
- @value 转换值float (方便转换) 像素
- @unit 转换类型
- */
- func unitConversion(value: Float, unit: KMBaseTextFieldUnit) -> Float {
- var scale: Float = 1.0
- var result = value
-
- switch unit {
- case .cm:
- scale = 595.0 / 21.0
- case .mm:
- scale = 595.0 / 210.0
- case .inch:
- scale = (595.0 / 21.0) * 2.54
- }
-
- result = result * scale
- return result
- }
-
- /**
- @abstract 数值转换
- @value 转换值float (方便转换) 参数
- @type 转换类型
- */
- func valueConversion(value: String, type: KMBaseTextFieldInputType) -> String {
- var string: NSString
-
- switch type {
- case .int:
- string = NSString(format: "%.0f", value)
- case .float:
- string = NSString(format: "%.1f", value)
- case .float_2:
- string = NSString(format: "%.2f", value)
- case .percent:
- string = NSString(format: "%.0f%", value)
- case .none:
- string = NSString(format: "%@", value)
- }
- return string as String
- }
- }
|