12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- //
- // KMBaseTextFormatter.swift
- // PDF Reader Pro
- //
- // Created by lizhe on 2022/12/12.
- //
- import Cocoa
- class KMBaseTextFormatter: NumberFormatter {
- var model: KMBaseTextFieldModel = KMBaseTextFieldModel()
- override func isPartialStringValid(_ partialStringPtr: AutoreleasingUnsafeMutablePointer<NSString>, proposedSelectedRange proposedSelRangePtr: NSRangePointer?, originalString origString: String, originalSelectedRange origSelRange: NSRange, errorDescription error: AutoreleasingUnsafeMutablePointer<NSString?>?) -> Bool {
- let data = self.model
- var partialString = partialStringPtr.pointee
- if partialString.debugDescription.isEmpty {
- return true
- }
- if (data.onlyNumber) {
- var regex: NSString = "^[0123456789.]*$"
- if !data.specialChart.debugDescription.isEmpty {
- regex = NSString(format: "^[0123456789%@]*$", data.specialChart)
- }
- let predicate = NSPredicate.init(format: "SELF MATCHES %@", regex)
- if !predicate.evaluate(with: partialString) {
- return false
- }
-
-
- if !data.specialChart.debugDescription.isEmpty {
- partialString = partialString.replacingOccurrences(of: data.specialChart, with: "") as NSString
- }
- if (Int(partialString.debugDescription) ?? 0 < data.minValue ||
- Int(partialString.debugDescription) ?? 0 > data.maxValue) {
- return false
- }
- }
-
- let newRange: NSRange = proposedSelRangePtr!.pointee
- if (NSMaxRange(origSelRange) > NSMaxRange(newRange)) {
- return true
- }
- return true
- }
-
- override func attributedString(for obj: Any, withDefaultAttributes attrs: [NSAttributedString.Key : Any]? = nil) -> NSAttributedString? {
- return nil
- }
- }
|